﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using System.IO;
using Telerik.WinControls.Data;
using System.Collections;
using log4net;
using Telerik.Data;
using Telerik.WinControls;
using Telerik.WinControls.UI.Export;
using System.Text.RegularExpressions;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;

namespace QuanLyNhaSach
{
    public partial class FormChinh2 : Form
    {
        //Tao Parttern Seleton
        #region "tạo singleton"
        static FormChinh2 doituong = null;
        public static FormChinh2 TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new FormChinh2();
            return doituong;
        }
        #endregion

        #region khai báo biến toàn cục
        private ILog logger;
        FileInfo file = new FileInfo(Application.ExecutablePath.ToString());
        sv_QuanLyNhaSach.AdapterClient xuli = new sv_QuanLyNhaSach.AdapterClient();
        // DirectoryInfo dr;
        //  frmhelp help = null;
        public string User = "";
        public string MaNV_FromMain = "";
        public string TenTaiKhoan_frmMain = "";
        public string MaNhom_frmMain = "";
        public static string manhom;
        //phân quyền
        public bool them;
        public bool sua;
        public bool xoa;
        public bool trangthai = false;
        int add = 0, update = 0, delete = 0;
        ArrayList KiemTra_DanhSach = new ArrayList();
        //-----------
        int vitri;
        int stt = 0;
        double tongtien = 0;
        double tongtienhang = 0;
        double tienDaDua = 0;
        double tienthue = 0;
        double tienConLai = 0;
        string chietkhau;
        float mucThue;
        string mahang;
        string tenhang;
        string dongia;
        string soluong;
        string thanhtien;
        public string manv = frmDangNhap.maNV_FromDangNhap;
        //--------------
        DataSet ds_khachang;
        string tennv = frmDangNhap.hoTenNV_FormDangNhap;
        #endregion

        public FormChinh2()
        {
            InitializeComponent();
           this.bt_dangnhap.Shortcuts.Add(new Telerik.WinControls.RadShortcut(Keys.Control, Keys.D));
           this.bt_thoat.Shortcuts.Add(new Telerik.WinControls.RadShortcut(Keys.Control, Keys.E));
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lbe_thongbao.Text = "Chưa đăng nhập";
            KhoaControl();
            //navigationPane1.SelectedPanel = buttonItem1;
        }

        #region "Xử lý thao tác hiện các Form"
        //Thanh hệ thống
        #region "Thanh hệ thống"
        private void bt_dangnhap_Click(object sender, EventArgs e)
        {
            if (bt_dangnhap.Text == "Đăng xuất")
            {
                if (MessageBox.Show("Bạn muốn đăng xuất không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Còn form nào là form con đang mở thì đóng frm đó
                    foreach (Form item in this.MdiChildren)
                    {
                        Application.OpenForms[item.Name].Close();
                    }

                    int vitri = Application.OpenForms.Count;//Lay số lượng các frm còn lại :

                    //Các form còn lại này sẽ không phải là frm con , đoạn code sau đây sẽ đóng các frm đó lại
                    if (Application.OpenForms != null)
                    {
                        for (int i = vitri - 1; i >= 0; i--)
                        {
                            if (Application.OpenForms[i] != Application.OpenForms["FormChinh2"])
                            {
                                Application.OpenForms[i].Close();
                            }
                        }
                    }
                    //Tao lai Hinh va Button Danh Nhap
                    bt_dangnhap.Image = global::QuanLyNhaSach.Properties.Resources.login;
                    bt_dangnhap.Text = "Đăng nhập";
                    belbl_MaNV.Text = "0";
                    betxt_HoTen.Text = "0";
                    lbe_thongbao.Text = "Chưa đăng nhập";
                    frmDangNhap.Kiemtra = false;
                    KhoaControl();

                    //HIện . Tắt các tab control
                    tabControl1.Visible = true;
                    tabControl1.SelectedTabIndex = 0;
                    tab_luachonnhanh.Visible = true;
                    tab_dmnhanvien.Visible = false;
                    tab_cauhinhhethong.Visible = false;
                    tab_phanquyen.Visible = false;
                    tab_dmnhanvien.Visible = false;
                    tab_dmkho.Visible = false;
                    tab_dmmathang.Visible = false;
                    tab_dmnhomhang.Visible = false;
                    tab_dmkhachhang.Visible = false;
                    tab_dmnhacungcap.Visible = false;
                    tab_thongtinnguoidung.Visible = false;
                    tab_thietlapkhuyenmai.Visible = false;
                    tab_Baogia.Visible = false;
                    AnControl_LuaChonNhanh();
                }
            }
            else
            {
                frmDangNhap dn = frmDangNhap.TaoDoiTuong();
                dn.StartPosition = FormStartPosition.CenterScreen;
                if (dn.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    if (frmDangNhap.Kiemtra == true)
                    {
                        belbl_MaNV.Text = frmDangNhap.maNV_FromDangNhap;
                        betxt_HoTen.Text = frmDangNhap.hoTenNV_FormDangNhap;
                        lbe_thongbao.Text = "Chào mừng bạn đã đăng nhập chương trình";

                        MaNV_FromMain = frmDangNhap.maNV_FromDangNhap;
                        TenTaiKhoan_frmMain = frmDangNhap.tenTaiKhoan_FrmDanhNhap;
                        MaNhom_frmMain = frmDangNhap.maNhom_FrmDangNhap;

                        bt_dangnhap.Text = "Đăng xuất";
                        bt_dangnhap.Image = global::QuanLyNhaSach.Properties.Resources.logout;

                        //Show control
                        manhom = frmDangNhap.maNhom_FrmDangNhap;
                        ShowControl(manhom);
                        ShowControl_LuaChonNhanh();
                    }
                }
            }
        }
        private void AnControl_LuaChonNhanh()
        {
            btnTracuu_dathangncc_Quick.Enabled = false;
            btnTracuu_khdathang_Quick.Enabled = false;
            btnTracuu_khtrahang_Quick.Enabled = false;
            btnTracuu_trahangncc_Quick.Enabled = false;
            btnTracuu_dieuchinhkho_Quick.Enabled = false;
            btnTracuu_hoadonnhap_Quick.Enabled = false;
            btnTracuu_hoadonbanle_Quick.Enabled = false;
            btnTracuu_hoadonbansi_Quick.Enabled = false;
            btnTracuu_phieuthu_Quick.Enabled = false;
            btnTracuu_phieuchi_Quick.Enabled = false;
        }
        private void ShowControl_LuaChonNhanh()
        {
            btnTracuu_dathangncc_Quick.Enabled = true;
            btnTracuu_khdathang_Quick.Enabled = true;
            btnTracuu_khtrahang_Quick.Enabled = true;
            btnTracuu_trahangncc_Quick.Enabled = true;
            btnTracuu_dieuchinhkho_Quick.Enabled = true;
            btnTracuu_hoadonnhap_Quick.Enabled = true;
            btnTracuu_hoadonbanle_Quick.Enabled = true;
            btnTracuu_hoadonbansi_Quick.Enabled = true;
            btnTracuu_phieuthu_Quick.Enabled = true;
            btnTracuu_phieuchi_Quick.Enabled = true;
        }
        private void timer_Gio_Tick(object sender, EventArgs e)
        {
            if (frmDangNhap.Kiemtra == true)
            {
                radLabelElement19.Text = DateTime.Now.Date.ToShortDateString() + " - " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
            }
        }
        private void bt_thoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void bt_saoluu_Click(object sender, EventArgs e)
        {
            frmBackupRestore rt = frmBackupRestore.TaoDoiTuong();
            rt.ShowDialog();
        }
        private void bt_thongtindonvi_Click(object sender, EventArgs e)
        {
            frmThongTinDonVi rt = frmThongTinDonVi.TaoDoiTuong();
            rt.ShowDialog();
        }
        private void lkl_DoiMatKhau_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmDoiMatKhau frmDMK = frmDoiMatKhau.TaoDoiTuong();
            frmDMK.ShowDialog();
        }
        private void bt_nhatkyht_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTabIndex = 1;
            tab_nhatkyhethong.Visible = true;
        }
        private void bt_cauhinhht_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTabIndex = 2;
            tab_cauhinhhethong.Visible = true;
        }
        private void bt_help_Click(object sender, EventArgs e)
        {

        }
        #endregion
        //Thanh Mua Hàng
        #region "thanh mua hàng"
        private void bt_phieudatncc_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmPhieuDatHangNCC rt = frmPhieuDatHangNCC.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private DataSet LayDS_ChiTietPhanQuyen(string manhom, string maform)
        {
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(new StringReader(xuli.selectqueryByText("select * from ChiTietPhanQuyen where MaNhom = '" + manhom + "' and MaForm = '" + maform + "'")));
            }
            catch (Exception ex)
            {
                logger.Error("Error: "+ex.Message);
                MessageBox.Show("Chưa bật Service. Vui lòng kiểm tra lại.","Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (ds.Tables.Count > 0)
                return ds;
            else
                return null;
        }
        private void bt_hoadonnhaphang_Click(object sender, EventArgs e)
        {
            frmHoaDonMuaHang hdmh = frmHoaDonMuaHang.TaoDoiTuong();
            hdmh.Activate();
            hdmh.Show();

        }
        private void bt_phieutrancc_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmTraHangNhaCungCap rt = frmTraHangNhaCungCap.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_bc_tonghopnhaphang_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmBC_Chitietnhapkho rt = frmBC_Chitietnhapkho.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_bc_tonghtncc_Click(object sender, EventArgs e)
        {
                this.Cursor = Cursors.WaitCursor;
                frmBC_TraHangNCC rt = frmBC_TraHangNCC.TaoDoiTuong();
                rt.Activate();
                rt.Show();
                this.Cursor = Cursors.Default;  
        }
        private void bt_bc_tongddhncc_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
                frmBC_PhieuDatHangNCC rt = frmBC_PhieuDatHangNCC.TaoDoiTuong();
                rt.Activate();
                rt.Show();
                this.Cursor = Cursors.Default;
        }
        private void bt_dmnhacungcap_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTab = tab_dmnhacungcap;
            tab_dmnhacungcap.Visible = true;
            #region phanquyen
            DataSet ds = new DataSet();
            manhom = frmDangNhap.maNhom_FrmDangNhap;
            ds = LayDS_ChiTietPhanQuyen(manhom, "4");
            if (ds.Tables[0].Rows[0][5].ToString() == "true")
            {
                bt_dmnhacungcap.Enabled = true;
                them = true;
                xoa = true;
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "true")
            {
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "false")
            {
                sua = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "true")
            {
                xoa = true;
            }
            if  (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "false")
            {
                xoa= false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "true")
            {
                them = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "false")
            {
                them = false;
            }
            Load_DMNhaCungCap();
            #endregion
        }
        #endregion
        //thanh ban hang
        #region "thanh bán hàng"
        private void bt_dmkhachhang_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTabIndex = 8;
            tab_dmkhachhang.Visible = true;
            #region phanquyen
            DataSet ds = new DataSet();
            manhom = frmDangNhap.maNhom_FrmDangNhap;
            ds = LayDS_ChiTietPhanQuyen(manhom, "1");
            if (ds.Tables[0].Rows[0][5].ToString() == "true")
            {
                bt_dmnhacungcap.Enabled = true;
                them = true;
                xoa = true;
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "true")
            {
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "false")
            {
                sua = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "true")
            {
                xoa = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "false")
            {
                xoa = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "true")
            {
                them = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "false")
            {
                them = false;
            }
            Load_DMKhachHang();
            #endregion
        }
        private void bt_hoadonbanle_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmHoaDonBanLe rt = frmHoaDonBanLe.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_phieukhdathang_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmPhieuDatHang rt = frmPhieuDatHang.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_hoadonbansi_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmHoaDonBanSi rt = frmHoaDonBanSi.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_khtrahang_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmHangBanTraLai rt = frmHangBanTraLai.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_bc_tongkhdathang_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmBC_PhieuKHDatHang khdh = frmBC_PhieuKHDatHang.TaoDoiTuong();
            khdh.Activate();
            khdh.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_bc_tongkhtrahang_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmBC_HangBanTraLai hbtl = frmBC_HangBanTraLai.TaoDoiTuong();
            hbtl.Activate();
            hbtl.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_bc_tongbanhang_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmBC_ChiTietBanHang ctbh = frmBC_ChiTietBanHang.TaoDoiTuong();
            ctbh.Activate();
            ctbh.Show();
            this.Cursor = Cursors.Default;
        }
        #endregion
        //Thanh Quản trị người dùng
        #region "Thanh quản trị người dùng"
        private void bt_nhanvien_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTab = tab_dmnhanvien;
            tab_dmnhanvien.Visible = true;
            #region phanquyen
            DataSet ds = new DataSet();
            manhom = frmDangNhap.maNhom_FrmDangNhap;
            ds = LayDS_ChiTietPhanQuyen(manhom, "5");
            if (ds.Tables[0].Rows[0][5].ToString() == "true")
            {
                bt_dmnhacungcap.Enabled = true;
                them = true;
                xoa = true;
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "true")
            {
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "false")
            {
                sua = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "true")
            {
                xoa = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "false")
            {
                xoa = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "true")
            {
                them = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "false")
            {
                them = false;
            }
            Load_NhanVien(); ;
            #endregion
        }
        private void bt_thongtinnguoidung_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tab_thongtinnguoidung;
            tab_thongtinnguoidung.Visible = true;
            Load_Thongtinnguoidung();
        }
        private void bt_quanlytaikhoan_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmQuanLyNguoiDung qlnd = frmQuanLyNguoiDung.TaoDoiTuong();
            qlnd.Activate();
            qlnd.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_phanquyen_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTabIndex = 3;
            tab_phanquyen.Visible = true;
            LoadPhanQuyen();
            LoadNhomQuyen();
            loadchitietphanquyen();
        }
        private void bt_doimatkhau_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau dmk = frmDoiMatKhau.TaoDoiTuong();            
            dmk.ShowDialog();
        }
        #endregion
        //Thanh PHIẾU THU - PHIẾU CHI
        #region "Thanh PHIẾU THU - PHIẾU CHI"
        private void bt_phieuchi_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmPhieuChi rt = frmPhieuChi.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_phieuthu_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmPhieuThu rt = frmPhieuThu.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        #endregion
        //Thanh Quản lý kho
        #region "quản lý kho"
        private void bt_dmkho_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTabIndex = 5;
            tab_dmkho.Visible = true;
            #region phanquyen
            DataSet ds = new DataSet();
            manhom = frmDangNhap.maNhom_FrmDangNhap;
            ds = LayDS_ChiTietPhanQuyen(manhom, "3");
            if (ds.Tables[0].Rows[0][5].ToString() == "true")
            {
                bt_dmnhacungcap.Enabled = true;
                them = true;
                xoa = true;
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "true")
            {
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "false")
            {
                sua = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "true")
            {
                xoa = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "false")
            {
                xoa = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "true")
            {
                them = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "false")
            {
                them = false;
            }
            Load_DMKho();

            #endregion

        }
        private void bt_dmmathang_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTab = tab_dmmathang;
            tab_dmmathang.Visible = true;
            #region phanquyen
            DataSet ds = new DataSet();
            manhom = frmDangNhap.maNhom_FrmDangNhap;
            ds = LayDS_ChiTietPhanQuyen(manhom, "3");
            if (ds.Tables[0].Rows[0][5].ToString() == "true")
            {
                bt_dmnhacungcap.Enabled = true;
                them = true;
                xoa = true;
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "true")
            {
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "false")
            {
                sua = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "true")
            {
                xoa = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "false")
            {
                xoa = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "true")
            {
                them = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "false")
            {
                them = false;
            }
            Load_DMHangHoa();

            #endregion

        }
        private void bt_dmnhomhang_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTab = tab_dmnhomhang;
            tab_dmnhomhang.Visible = true;
            #region phanquyen
            DataSet ds = new DataSet();
            manhom = frmDangNhap.maNhom_FrmDangNhap;
            ds = LayDS_ChiTietPhanQuyen(manhom, "6");
            if (ds.Tables[0].Rows[0][5].ToString() == "true")
            {
                bt_dmnhacungcap.Enabled = true;
                them = true;
                xoa = true;
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "true")
            {
                sua = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Sua"].ToString() == "false")
            {
                sua = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "true")
            {
                xoa = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Xoa"].ToString() == "false")
            {
                xoa = false;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "true")
            {
                them = true;
            }
            if (ds.Tables[0].Rows[0][5].ToString() != "true" && ds.Tables[0].Rows[0]["Them"].ToString() == "false")
            {
                them = false;
            }
            Load_DMNhomHang();
            #endregion
        }
        private void bt_phieudieuchinhkho_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmDieuChinhKho rt = frmDieuChinhKho.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        #endregion
        //Thanh BÁO CÁO
        #region "BÁO CÁO"
        private void bt_baocaolailo_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmBc_DoanhThuBanHang fdoanhthu = frmBc_DoanhThuBanHang.TaoDoiTuong();
            fdoanhthu.Activate();
            fdoanhthu.Show();
            this.Cursor = Cursors.Default;
        
        }
        private void bt_baocaotheothang_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmBC_CongNoKhachHang fCongNo = frmBC_CongNoKhachHang.TaoDoiTuong();
            fCongNo.Activate();
            fCongNo.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_baocaotheongay_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmBC_TongHopDoanhThuBanHang fxuatnhapton = frmBC_TongHopDoanhThuBanHang.TaoDoiTuong();
            fxuatnhapton.Activate();
            fxuatnhapton.Show();
            this.Cursor = Cursors.Default;

        }
        #endregion
        //Thanh TIỆN ÍCH
        #region "TIỆN ÍCH"
        private void bt_tienich_baogiakh_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTab = tab_Baogia;
            tab_Baogia.Visible = true;

        }
        private void bt_tienich_inmavach_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmTienIch_InMaVach inma = frmTienIch_InMaVach.TaoDoiTuong();
            inma.Show();
            this.Cursor = Cursors.Default;
        }
        private void bt_tienich_thietlapkm_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tab_thietlapkhuyenmai;
            tab_thietlapkhuyenmai.Visible = true;
        }
        #endregion
        //Thanh Admin
        #region "Thanh chọn nhanh cho Admin"
        private void thanhadmin1_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTabIndex = 2;
            tab_cauhinhhethong.Visible = true;
        }
        private void thanhadmin2_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTabIndex = 1;
            tab_nhatkyhethong.Visible = true;
        }
        private void thanhadmin3_Click(object sender, EventArgs e)
        {
            frmBackupRestore rt = frmBackupRestore.TaoDoiTuong();
            rt.ShowDialog();
        }
        private void thanhadmin4_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmQuanLyNguoiDung qlnd = frmQuanLyNguoiDung.TaoDoiTuong();
            qlnd.Activate();
            qlnd.Show();
            this.Cursor = Cursors.Default;
        }
        private void thanhadmin5_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            tabControl1.SelectedTabIndex = 3;
            tab_phanquyen.Visible = true;
            LoadPhanQuyen();
            LoadNhomQuyen();
            loadchitietphanquyen();
        }
        #endregion

        #endregion

        #region "Control button"
        private void KhoaControl()
        {
            be_trangthai.Text = "Trạng Thái Đăng Nhập";
            lbe_thongbao.Text = "Chưa Đăng Nhập";
            bt_dangnhap.Enabled = true;
            bt_saoluu.Enabled = false;
            bt_nhatkyht.Enabled = false;
            bt_cauhinhht.Enabled = false;
            bt_thongtindonvi.Enabled = false;

            bt_quanlytaikhoan.Enabled = false;
            bt_phanquyen.Enabled = false;
            bt_nhanvien.Enabled = false;
            bt_thongtinnguoidung.Enabled = false;
            bt_doimatkhau.Enabled = false;

            bt_dmkho.Enabled = false;
            bt_dmmathang.Enabled = false;
            bt_dmnhomhang.Enabled = false;
            bt_phieudieuchinhkho.Enabled = false;

            bt_dmncc.Enabled = false;
            bt_phieudatncc.Enabled = false;
            bt_hoadonnhaphang.Enabled = false;
            bt_phieutrancc.Enabled = false;
            bt_bc_tongnhaphang.Enabled = false;
            bt_bc_tongddhncc.Enabled = false;
            bt_bc_tonghtncc.Enabled = false;

            bt_dmkhachhang.Enabled = false;
            bt_hoadonbanle.Enabled = false;
            bt_hoadonbansi.Enabled = false;
            bt_phieukhdathang.Enabled = false;
            bt_khtrahang.Enabled = false;
            bt_bc_tongbanhang.Enabled = false;
            bt_bc_tongkhdathang.Enabled = false;
            bt_bc_tongkhtrahang.Enabled = false;

            bt_phieuthu.Enabled = false;
            bt_phieuchi.Enabled = false;

            bt_baocaotheongay.Enabled = false;
            bt_baocaotheothang.Enabled = false;
            bt_baocaolailo.Enabled = false;

            bt_tienich_baogiakh.Enabled = false;
            bt_tienich_inmavach.Enabled = false;
            bt_tienich_thietlapkm.Enabled = false;

            navigationPane1.Enabled = false;
        }
        #endregion

        #region "Show control"
        private void ShowControl(string manhom)
        {
            #region 1: Danh muc khach hang
            if (LayDS_ChiTietPhanQuyen(manhom, "1") != null)
            {
                bt_dmkhachhang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "Admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_dmkhachhang.Enabled = true;
                }
            }
            #endregion

            #region 2: Danh muc hang hoa
            if (LayDS_ChiTietPhanQuyen(manhom, "2") != null)
            {
                bt_dmmathang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_dmmathang.Enabled = true;
                }
            }
            #endregion

            #region 3: Danh muc kho
            if (LayDS_ChiTietPhanQuyen(manhom, "3") != null)
            {
                bt_dmkho.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_dmkho.Enabled = true;
                }
            }
            #endregion

            #region 4: Danh muc nha cung cap
            if (LayDS_ChiTietPhanQuyen(manhom, "4") != null)
            {
                bt_dmncc.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_dmncc.Enabled = true;
                }
            }
            #endregion

            #region 5: Danh muc nhan vien
            if (LayDS_ChiTietPhanQuyen(manhom, "5") != null)
            {
                bt_nhanvien.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_nhanvien.Enabled = true;
                }
            }
            #endregion

            #region 6: Danh muc nhom hang
            if (LayDS_ChiTietPhanQuyen(manhom, "6") != null)
            {
                bt_dmnhomhang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_dmnhomhang.Enabled = true;
                }
            }
            #endregion

            #region 7: Phieu dieu chinh kho
            if (LayDS_ChiTietPhanQuyen(manhom, "7") != null)
            {
                bt_phieudieuchinhkho.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_phieudieuchinhkho.Enabled = true;
                }
            }
            #endregion

            #region 8: Hoa don ban le
            if (LayDS_ChiTietPhanQuyen(manhom, "8") != null)
            {
                bt_hoadonbanle.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_hoadonbanle.Enabled = true;
                }
            }
            #endregion

            #region 9: HOa don ban si
            if (LayDS_ChiTietPhanQuyen(manhom, "9") != null)
            {
                bt_hoadonbansi.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_hoadonbansi.Enabled = true;
                }
            }
            #endregion

            #region 10: HOa don nhap hang
            if (LayDS_ChiTietPhanQuyen(manhom, "10") != null)
            {
                bt_hoadonnhaphang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_hoadonnhaphang.Enabled = true;
                }
            }
            #endregion

            #region 11: Phieu tra hang nha cung cap
            if (LayDS_ChiTietPhanQuyen(manhom, "12") != null)
            {
                bt_phieutrancc.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_phieutrancc.Enabled = true;
                }
            }
            #endregion

            #region 12: Phieu khach hang tra hang
            if (LayDS_ChiTietPhanQuyen(manhom, "12") != null)
            {
                bt_khtrahang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_khtrahang.Enabled = true;
                }
            }
            #endregion

            #region 13: Phieu thu
            if (LayDS_ChiTietPhanQuyen(manhom, "13") != null)
            {
                bt_phieuthu.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_phieuthu.Enabled = true;
                }
            }
            #endregion

            #region 14: Phieu chi
            if (LayDS_ChiTietPhanQuyen(manhom, "14") != null)
            {
                bt_phieuchi.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_phieuchi.Enabled = true;
                }
            }
            #endregion

            #region 15: Phieu khach hang dat hang
            if (LayDS_ChiTietPhanQuyen(manhom, "15") != null)
            {
                bt_phieukhdathang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_phieukhdathang.Enabled = true;
                }
            }
            #endregion

            #region 16: Phieu dat nha cung cap
            if (LayDS_ChiTietPhanQuyen(manhom, "16") != null)
            {
                bt_phieudatncc.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_phieudatncc.Enabled = true;
                }
            }
            #endregion

            #region 17: Phan quyen
            if (LayDS_ChiTietPhanQuyen(manhom, "17") != null)
            {
                bt_phanquyen.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_phanquyen.Enabled = true;
                }
            }
            #endregion

            #region 18: quan ly nguoi dung
            if (LayDS_ChiTietPhanQuyen(manhom, "18") != null)
            {
                bt_quanlytaikhoan.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_quanlytaikhoan.Enabled = true;
                }
            }
            #endregion

            #region 19: Backup and restore
            if (LayDS_ChiTietPhanQuyen(manhom, "19") != null)
            {
                bt_saoluu.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_saoluu.Enabled = true;
                }
            }
            #endregion

            #region 20: bao cao chi tiet ban hang
            if (LayDS_ChiTietPhanQuyen(manhom, "20") != null)
            {
                bt_bc_tongbanhang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_bc_tongbanhang.Enabled = true;
                }
            }
            #endregion

            #region 21: bao cao chi tiet nhap hang
            if (LayDS_ChiTietPhanQuyen(manhom, "21") != null)
            {
                bt_bc_tongnhaphang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_bc_tongnhaphang.Enabled = true;
                }
            }

            #endregion

            #region 22: bao cao don dat hang hang NCC
            if (LayDS_ChiTietPhanQuyen(manhom, "22") != null)
            {
                bt_bc_tongddhncc.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_bc_tongddhncc.Enabled = true;
                }
            }
            #endregion

            #region 23: Bao cao hang ban tra lai
            if (LayDS_ChiTietPhanQuyen(manhom, "23") != null)
            {
                bt_bc_tongkhtrahang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_bc_tongkhtrahang.Enabled = true;
                }
            }
            #endregion

            #region 24: Bao cao hang tra nha cung cap
            if (LayDS_ChiTietPhanQuyen(manhom, "24") != null)
            {
                bt_bc_tonghtncc.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_bc_tonghtncc.Enabled = true;
                }
            }
            #endregion

            #region 25: bao cao tong hop doanh thu ban hang
            if (LayDS_ChiTietPhanQuyen(manhom, "25") != null)
            {
                bt_baocaotheongay.Enabled = true;
                bt_baocaotheothang.Enabled = true;
                bt_baocaolailo.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_baocaotheongay.Enabled = true;
                    bt_baocaotheothang.Enabled = true;
                    bt_baocaolailo.Enabled = true;
                }
            }
            #endregion

            #region 26: Nh?t ký h? th?ng
            if (LayDS_ChiTietPhanQuyen(manhom, "26") != null)
            {
                bt_nhatkyht.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_nhatkyht.Enabled = true;
                }
            }
            #endregion

            #region 27: C?u hình h? th?ng
            if (LayDS_ChiTietPhanQuyen(manhom, "27") != null)
            {
                bt_cauhinhht.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_cauhinhht.Enabled = true;
                }
            }
            #endregion

            #region 28: Thông tin don v?
            if (LayDS_ChiTietPhanQuyen(manhom, "28") != null)
            {
                bt_thongtindonvi.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_thongtindonvi.Enabled = true;
                }
            }
            #endregion

            //chua t?o form
            #region 29: Thông tin ngu?i dùng
            if (LayDS_ChiTietPhanQuyen(manhom, "29") != null)
            {
                bt_thongtinnguoidung.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_thongtinnguoidung.Enabled = true;
                }
            }
            #endregion

            #region 30: Doi m?t kh?u
            if (LayDS_ChiTietPhanQuyen(manhom, "30") != null)
            {
                bt_doimatkhau.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_doimatkhau.Enabled = true;
                }
            }
            #endregion

            #region 31: Báo cáo phi?u khách hàng d?t hàng
            if (LayDS_ChiTietPhanQuyen(manhom, "31") != null)
            {
                bt_bc_tongkhdathang.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_bc_tongkhdathang.Enabled = true;
                }
            }
            #endregion

            #region 32: báo giá khách hàng
            if (LayDS_ChiTietPhanQuyen(manhom, "32") != null)
            {
                bt_tienich_baogiakh.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_tienich_baogiakh.Enabled = true;
                }
            }

            #endregion

            #region 33: In ma vach
            if (LayDS_ChiTietPhanQuyen(manhom, "33") != null)
            {
                bt_tienich_inmavach.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_tienich_inmavach.Enabled = true;
                }
            }
            #endregion

            #region 34: Thi?t l?p khuy?n mãi
            if (LayDS_ChiTietPhanQuyen(manhom, "34") != null)
            {
                bt_tienich_thietlapkm.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    bt_tienich_thietlapkm.Enabled = true;
                }
            }
            #endregion

            #region 35: Thanh Admin
            if (LayDS_ChiTietPhanQuyen(manhom, "35") != null)
            {
                navigationPane1.Enabled = true;
            }
            else
            {
                if ((frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456") || (frmDangNhap.maNV_FromDangNhap == "admin" && frmDangNhap.tenTaiKhoan_FrmDanhNhap == "123456"))
                {
                    navigationPane1.Enabled = true;
                }
            }
            #endregion

        }
        #endregion

        #region "Xử lý các sự kiện Load"
        //---------------------------
        private void tabControl1_SelectedTabChanged(object sender, DevComponents.DotNetBar.TabStripTabChangedEventArgs e)
        {
            if (tabControl1.SelectedTab == tab_nhatkyhethong)
            {

            }
            else if (tabControl1.SelectedTab == tab_cauhinhhethong)
            {

            }
            else if (tabControl1.SelectedTab == tab_phanquyen)
            {
                LoadNhomQuyen();
                loadchitietphanquyen();
            }
            else if (tabControl1.SelectedTab == tab_dmnhanvien)
            {
                Load_NhanVien();
            }
            else if (tabControl1.SelectedTab == tab_dmkho)
            {
                Load_DMKho();
            }
            else if (tabControl1.SelectedTab == tab_dmmathang)
            {
                Load_DMHangHoa();
            }
            else if (tabControl1.SelectedTab == tab_dmnhomhang)
            {
                Load_DMNhomHang();
            }
            else if (tabControl1.SelectedTab == tab_dmkhachhang)
            {
                Load_DMKhachHang();
            }
            else if (tabControl1.SelectedTab == tab_dmnhacungcap)
            {
                Load_DMNhaCungCap();
            }
            else if (tabControl1.SelectedTab == tab_thongtinnguoidung)
            {

            }
            else if (tabControl1.SelectedTab == tab_Baogia)
            {
             
            }
            else if (tabControl1.SelectedTab == tab_thietlapkhuyenmai)
            {
            
            }

        }
        private void tabControl1_TabItemClose(object sender, DevComponents.DotNetBar.TabStripActionEventArgs e)
        {
            if (tabControl1.SelectedTab == tab_nhatkyhethong)
            {
                tab_nhatkyhethong.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_cauhinhhethong)
            {
                tab_cauhinhhethong.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_phanquyen)
            {
                tab_phanquyen.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_dmnhanvien)
            {
                tab_dmnhanvien.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_dmkho)
            {
                tab_dmkho.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_dmmathang)
            {
                tab_dmmathang.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_dmnhomhang)
            {
                tab_dmnhomhang.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_dmkhachhang)
            {
                tab_dmkhachhang.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_dmnhacungcap)
            {
                tab_dmnhacungcap.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_thongtinnguoidung)
            {
                tab_thongtinnguoidung.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_Baogia)
            {
                tab_Baogia.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }
            else if (tabControl1.SelectedTab == tab_thietlapkhuyenmai)
            {
                tab_thietlapkhuyenmai.Visible = false;
                tabControl1.SelectedTab = tab_luachonnhanh;
            }


        }
        #endregion

        #region "xu lý các sự kiện click Tab CONTROL"
        bool openExportFile = false;
        private bool exportVisualSettings;
        
        //-----------------------
        #region tab nhat ký hệ thống
        #endregion
        //-----------------------
        #region "Tab: Cấu hình hệ thống"
        #endregion
        //-----------------------
        #region "Tab: Phân quyền"
        DataTable bang_tam;
        private void LoadPhanQuyen()
        {
            try
            {
                //Tao Cột cho Bảng Tạm
                bang_tam = new DataTable();
                bang_tam.Columns.Add("MaNhom", typeof(string));
                bang_tam.Columns.Add("MaForm", typeof(string));
                bang_tam.Columns.Add("Them", typeof(bool));
                bang_tam.Columns.Add("Xoa", typeof(bool));
                bang_tam.Columns.Add("Sua", typeof(bool));
                bang_tam.Columns.Add("Toanquyen", typeof(bool));

                logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                LoadNhomQuyen();
                loadchitietphanquyen();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        private void LoadNhomQuyen()
        {
            //Chinh do dai cua cac cot trong muiltcombobox
            RadMultiColumnComboBoxElement multiColumnComboElement = this.cmb_nhomquyen.MultiColumnComboBoxElement;
            multiColumnComboElement.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
            multiColumnComboElement.DropDownMinSize = new Size(200, 100);
            //load nhom quyen len combobox
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(xuli.selectqueryByText("select manhom,tennhom from NhomQuyen")));
            if (ds.Tables.Count > 0)
            {
                ds.Tables[0].Columns.Add("STT", typeof(int));
                DataRow dr;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dr = ds.Tables[0].NewRow();
                    dr["STT"] = i + 1;
                    ds.Tables[0].Rows[i]["STT"] = dr["STT"].ToString();
                    //Xoa bỏ quyền Admin
                    if ("1" == ds.Tables[0].Rows[i]["MaNhom"].ToString().Trim())
                        ds.Tables[0].Rows[i].Delete();
                }

                cmb_nhomquyen.DataSource = ds.Tables[0];
                cmb_nhomquyen.DisplayMember = "tennhom";
                cmb_nhomquyen.ValueMember = "manhom";
            }
            FilterDescriptor des_mh = new FilterDescriptor(this.cmb_nhomquyen.DisplayMember, FilterOperator.StartsWith, string.Empty);
            cmb_nhomquyen.EditorControl.FilterDescriptors.Add(des_mh);
        }
        private void cmb_nhomquyen_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                if (cmb_nhomquyen.Text.Trim() == "")
                    txt_manhomquyen.Text = "";
                else
                {
                    txt_manhomquyen.Text = cmb_nhomquyen.SelectedValue.ToString();

                    DataSet ds2 = new DataSet();
                    ds2.ReadXml(new StringReader(xuli.selectqueryByText("select * from ChiTietPhanQuyen where manhom='" + cmb_nhomquyen.SelectedValue.ToString() + "'")));
                    if (ds2.Tables.Count > 0)
                    {

                        ds2.Tables[0].Columns.Add("STT", typeof(int));
                        gv_chitietphanquyen.DataSource = ds2.Tables[0];
                        DataRow dr;
                        for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                        {
                            dr = ds2.Tables[0].NewRow();
                            dr["STT"] = i + 1;
                            ds2.Tables[0].Rows[i]["STT"] = dr["STT"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void loadchitietphanquyen()
        {
            //hiện tên form trên coblumm maform
            DataSet ds1 = new DataSet();
            ds1.ReadXml(new StringReader(xuli.selectqueryByText("select * from Form")));
            if (ds1.Tables.Count > 0)
            {
                Telerik.WinControls.UI.GridViewComboBoxColumn txtgrid = (Telerik.WinControls.UI.GridViewComboBoxColumn)gv_chitietphanquyen.Columns[2];
                txtgrid.DataSource = ds1.Tables[0];
                txtgrid.DisplayMember = "tenform";
                txtgrid.ValueMember = "maform";
            }
        }
        private void bt_lamtuoi_Click(object sender, EventArgs e)
        {
            try
            {
                if (bang_tam.Rows.Count > 0)
                {
                    for (int i = 0; i < bang_tam.Rows.Count; i++)
                    {
                        if (!xuli.UpdateCTPQ(bang_tam.Rows[i]["MaNhom"].ToString().Trim(), bang_tam.Rows[i]["MaForm"].ToString().Trim(), bool.Parse(bang_tam.Rows[i]["Them"].ToString()), bool.Parse(bang_tam.Rows[i]["Xoa"].ToString()), bool.Parse(bang_tam.Rows[i]["Sua"].ToString()), bool.Parse(bang_tam.Rows[i]["ToanQuyen"].ToString())))
                        {
                            MessageBox.Show("Cập nhật thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }

                }
                string manhom = gv_chitietphanquyen.CurrentRow.Cells[1].Value.ToString().Trim();
                string maform = gv_chitietphanquyen.CurrentRow.Cells[2].Value.ToString().Trim();
                bool them = kiem_traPhanQuyen(gv_chitietphanquyen.CurrentRow.Cells[3].Value.ToString().Trim());
                bool xoa = kiem_traPhanQuyen(gv_chitietphanquyen.CurrentRow.Cells[4].Value.ToString());
                bool sua = kiem_traPhanQuyen(gv_chitietphanquyen.CurrentRow.Cells[5].Value.ToString());
                bool toanquyen = kiem_traPhanQuyen(gv_chitietphanquyen.CurrentRow.Cells[6].Value.ToString());
                if (!xuli.UpdateCTPQ(manhom, maform, them, xoa, sua, toanquyen))
                {
                    MessageBox.Show("Cập nhật thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                MessageBox.Show("Cập nhật thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loadchitietphanquyen();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
            errorProvider1.Clear();
        }
        #endregion
        //-----------------------
        #region Tab: Danh mục Nhân Viên
        private void bt_themmoi_dmnv_Click(object sender, EventArgs e)
        {
            DataSet ds_tam = new DataSet();
            string mamoi = "";
            string maTam = "NV";
            try
            {
                ds_tam.ReadXml(new StringReader(xuli.selectqueryByText("select top 1 MaNV from NhanVien where MANV like '%" + maTam + "%'order by MANV desc")));
                if (ds_tam.Tables.Count == 0)
                    mamoi = maTam + "0001";
                else
                {
                    string matam = ds_tam.Tables[0].Rows[0][0].ToString();
                    string kituso = matam.Substring(2, 4).ToString();
                    int somoi = int.Parse(kituso) + 1;
                    mamoi = maTam + somoi.ToString("0000");
                }
                int dem = 0;
                int vitribatdau = gv_nhanvien.Rows.Count - 1;
                FormPhu_NhanVien frm = new FormPhu_NhanVien();
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    Load_BangNhanvien();
                }

                dem++;
                gv_nhanvien.CurrentRow.Cells[1].Value = mamoi.ToString();
                gv_nhanvien.Rows[gv_dmKH.Rows.Count - 1].Cells["HoTenNV"].IsSelected = true;

            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void Load_NhanVien()
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                this.AddSummariesNhanVien();

                if (them == false)
                    bt_themmoi_dmnv.Enabled = false;
                else
                    bt_themmoi_dmnv.Enabled = true;

                if (sua == false)
                    bt_hieuchinh_dmnv.Enabled = false;
                else
                    bt_hieuchinh_dmnv.Enabled = true;

                if (xoa == false)
                    bt_xoa_dmnv.Enabled = false;
                else
                    bt_xoa_dmnv.Enabled = true;

                //load nhanh vien
                Load_BangNhanvien();

                gv_nhanvien.EnableCustomFiltering = false;
                gv_nhanvien.ShowFilteringRow = false;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void Load_BangNhanvien()
        {
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(new StringReader(xuli.LayDS_NhanVien()));
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Add("stt", typeof(int));
                    gv_nhanvien.DataSource = ds.Tables[0];
                    DataRow dr;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dr = ds.Tables[0].NewRow();
                        dr["stt"] = i + 1;
                        ds.Tables[0].Rows[i]["stt"] = dr["stt"].ToString();
                        KiemTra_DanhSach.Add(ds.Tables[0].Rows[i]["MaNV"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void AddSummariesNhanVien()
        {
            this.gv_nhanvien.MasterTemplate.SummaryRowsTop.Clear();
            this.gv_nhanvien.TableElement.BeginUpdate();
            this.gv_nhanvien.MasterTemplate.EnableFiltering = true;
            this.gv_nhanvien.MasterTemplate.AutoExpandGroups = true;
            this.gv_nhanvien.MasterTemplate.AllowAddNewRow = false;
            this.gv_nhanvien.TableElement.EndUpdate();
            GridViewSummaryRowItem item1 = new GridViewSummaryRowItem();
            item1.Add(new GridViewSummaryItem("HoTenNV", "Tổng nhân viên: {0}", GridAggregateFunction.Count));
            this.gv_nhanvien.MasterTemplate.SummaryRowsTop.Add(item1);
        }
        private void bt_hieuchinh_dmnv_Click(object sender, EventArgs e)
        {
            try
            {
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_themmoi_dmnv.Enabled = false;
                        bt_xoa_dmnv.Enabled = false;
                    }
                    else
                    {
                        if (them == false)
                            bt_themmoi_dmnv.Enabled = false;
                        else
                            bt_themmoi_dmnv.Enabled = false;

                        if (xoa == false)
                            bt_xoa_dmnv.Enabled = false;
                        else
                            bt_xoa_dmnv.Enabled = true;
                    }
                    //giao dien
                    bt_hieuchinh_dmnv.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_hieuchinh_dmnv.Text = "Lưu";
                    trangthai = true;
                    gv_nhanvien.ReadOnly = false;
                    gv_nhanvien.AllowEditRow = true;

                }
                else if (trangthai == true)
                {

                    //errorProvider1.Clear();
                    //Sua Thong Tin Khach Hang
                    if (Regex.IsMatch(gv_nhanvien.CurrentRow.Cells["SoDTNV"].Value.ToString().Trim(), @"^\d{8,11}$") == false)
                    {
                        MessageBox.Show("Số ĐT chỉ nhập số, độ dài từ 8->11 kí tự");
                    }
                    else if (Regex.IsMatch(gv_nhanvien.CurrentRow.Cells["CMNDNV"].Value.ToString().Trim(), @"^\d{9,9}$") == false)
                    {
                        MessageBox.Show("Số CMND chỉ nhập số, độ dài là 9 kí tự");
                    }
                    string manv = gv_nhanvien.CurrentRow.Cells["MaNV"].Value.ToString().Trim();
                    string tennv = gv_nhanvien.CurrentRow.Cells["HoTenNV"].Value.ToString().Trim();
                    string sdt = gv_nhanvien.CurrentRow.Cells["SoDTNV"].Value.ToString().Trim();
                    DateTime ngaysinh = DateTime.Parse(gv_nhanvien.CurrentRow.Cells["NgaySinhNV"].Value.ToString().Trim());
                    string diachi = gv_nhanvien.CurrentRow.Cells["DiaChiNV"].Value.ToString().Trim();
                    string cmnd = gv_nhanvien.CurrentRow.Cells["CMNDNV"].Value.ToString().Trim();
                    string chucvu = gv_nhanvien.CurrentRow.Cells["ChucVu"].Value.ToString().Trim();
                    if (!xuli.UpdateNV(manv, tennv, sdt, ngaysinh, diachi, cmnd, chucvu))
                    {
                        MessageBox.Show("Sửa thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    update++;
                    MessageBox.Show("Sửa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangNhanvien();

                    //giao diện
                    bt_hieuchinh_dmnv.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_hieuchinh_dmnv.Text = "Sửa";
                    trangthai = false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_xoa_dmnv_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn thực sự muốn xoá nhân viên này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string manv = gv_nhanvien.CurrentRow.Cells["MaNV"].Value.ToString();
                    if (!xuli.DeleteNV(manv))
                    {
                        MessageBox.Show("Xoá thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                        MessageBox.Show("Xoá thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangNhanvien();
                    delete++;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_ngung_dmnv_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();

                if (them == false)
                {
                    bt_themmoi_dmnv.Enabled = false;
                    bt_themmoi_dmnv.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_themmoi_dmnv.Text = "Thêm";
                    gv_nhanvien.AllowAddNewRow = false;
                }
                else
                {
                    bt_themmoi_dmnv.Enabled = true;
                    bt_themmoi_dmnv.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_themmoi_dmnv.Text = "Thêm";
                    gv_nhanvien.AllowAddNewRow = false;
                }

                if (sua == false)
                {
                    bt_hieuchinh_dmnv.Enabled = false;
                    bt_hieuchinh_dmnv.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_hieuchinh_dmnv.Text = "Sửa";
                    gv_nhanvien.AllowEditRow = false;
                }
                else
                {
                    bt_hieuchinh_dmnv.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_hieuchinh_dmnv.Text = "Sửa";
                    bt_hieuchinh_dmnv.Enabled = true;
                    gv_nhanvien.AllowEditRow = false;
                }

                if (xoa == false)
                    bt_xoa_dmnv.Enabled = false;
                else
                    bt_xoa_dmnv.Enabled = true;

                trangthai = false;
                Load_NhanVien();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_excel_dmnv_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog1.FileName.Equals(String.Empty))
            {
                RadMessageBox.SetThemeName(this.gv_nhanvien.ThemeName);
                RadMessageBox.Show("Làm ơn nhập tên file.");
                return;
            }
            string fileName = this.saveFileDialog1.FileName;

            ExportToExcelML excelExporter = new ExportToExcelML(this.gv_nhanvien);

            excelExporter.SheetMaxRows = ExcelMaxRows._1048576;
            excelExporter.ExportVisualSettings = this.exportVisualSettings;

            
            try
            {
                excelExporter.RunExport(fileName);

                RadMessageBox.SetThemeName(this.gv_nhanvien.ThemeName);
                DialogResult dr = RadMessageBox.Show("Dữ liệu trong Grid đã được in thành công. Bạn có muốn mở file này không?",
                    "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    openExportFile = true;
                }
            }
            catch (IOException ex)
            {
                RadMessageBox.SetThemeName(this.gv_nhanvien.ThemeName);
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
        private void bt_lamtuoi_dmnv_Click(object sender, EventArgs e)
        {
                Load_BangNhanvien();
        }
        private void bt_tim_dmnv_Click(object sender, EventArgs e)
        {
            try
            {
                if (bt_tim_dmnv.Text == "Trở lại")
                {
                    bt_tim_dmnv.Text = "Tìm kiếm";
                    bt_tim_dmnv.Image = global::QuanLyNhaSach.Properties.Resources.go_back_icon;
                    gv_nhanvien.EnableFiltering = false;
                    gv_nhanvien.ShowFilteringRow = false;
                }
                else if (bt_tim_dmnv.Text == "Tìm kiếm")
                {
                    bt_tim_dmnv.Text = "Trở lại";
                    bt_tim_dmnv.Image = global::QuanLyNhaSach.Properties.Resources.loc;
                    gv_nhanvien.EnableFiltering = true;
                    gv_nhanvien.ShowFilteringRow = true;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        #endregion
        //-----------------------
        #region Tab: Danh mục kho
        private void Load_DMKho()
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                if (them == false)
                    bt_them_dmkho.Enabled = false;
                else
                    bt_them_dmkho.Enabled = true;

                if (xoa == false)
                    bt_xoa_dmkho.Enabled = false;
                else
                    bt_xoa_dmkho.Enabled = true;

                if (sua == false)
                    bt_sua_dmkho.Enabled = false;
                else
                    bt_sua_dmkho.Enabled = true;

                Load_DanhSach_Kho();

                //giao dien

                this.AddSummarieskho();

                gvkho.EnableFiltering = false;
                gvkho.ShowFilteringRow = false;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void AddSummarieskho()
        {
            try
            {
                this.gvkho.MasterTemplate.SummaryRowsTop.Clear();
                this.gvkho.TableElement.BeginUpdate();
                this.gvkho.MasterTemplate.EnableFiltering = true;
                this.gvkho.MasterTemplate.AutoExpandGroups = true;
                this.gvkho.MasterTemplate.AllowAddNewRow = false;
                this.gvkho.TableElement.EndUpdate();
                GridViewSummaryRowItem item1 = new GridViewSummaryRowItem();
                item1.Clear();
                item1.Add(new GridViewSummaryItem("TenKho", "Tổng bản ghi: {0}", GridAggregateFunction.Count));
                this.gvkho.MasterTemplate.SummaryRowsTop.Add(item1);
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }
        private void Load_DanhSach_Kho()
        {
            try
            {
                KiemTra_DanhSach.Clear();
                DataSet ds = new DataSet();
                ds.ReadXml(new StringReader(xuli.LayDS_Kho()));
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Add("STT", typeof(int));

                    DataRow dr;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dr = ds.Tables[0].NewRow();
                        dr["STT"] = i + 1;
                        ds.Tables[0].Rows[i]["STT"] = dr["STT"].ToString();
                        KiemTra_DanhSach.Add(ds.Tables[0].Rows[i]["MaKho"].ToString());

                    }
                    gvkho.DataSource = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }


        }
        private void bt_close_dmkho_Click(object sender, EventArgs e)
        {
            tab_dmkho.Visible = false;
        }
        private void bt_them_dmkho_Click(object sender, EventArgs e)
        {
            try
            {
                if (bt_them_dmkho.Text != "Lưu")
                {
                    string mamoi = "";
                    DataSet ds_tam = new DataSet();
                    string maTam = "Kho";
                    sv_QuanLyNhaSach.AdapterClient xuly = new sv_QuanLyNhaSach.AdapterClient();
                    ds_tam.ReadXml(new StringReader(xuly.selectqueryByText("select top 1 MaKho from Kho where MaKho like '%" + maTam + "%'order by MaKho desc")));
                    if (ds_tam.Tables.Count == 0)
                        mamoi = maTam + "01";
                    else
                    {
                        string matam = ds_tam.Tables[0].Rows[0][0].ToString();
                        string kituso = matam.Substring(3, 2).ToString();
                        int somoi = int.Parse(kituso) + 1;
                        mamoi = maTam + somoi.ToString("00");
                    }
                    int dem = 0;
                    int vitribatdau = gv_dmnhacungcap.Rows.Count - 1;
                    //theo kiểu phân quyền
                    if (trangthai == false)
                    {
                        if (them == true && xoa == true && sua == true)
                        {
                            bt_sua_dmkho.Enabled = false;
                            bt_xoa_dmkho.Enabled = false;
                        }
                        else
                        {
                            if (sua == false)
                                bt_sua_dmkho.Enabled = false;
                            else
                                bt_sua.Enabled = false;

                            if (xoa == false)
                                bt_xoa_dmkho.Enabled = false;
                            else
                                bt_xoa_dmkho.Enabled = true;
                        }
                        //giao dien
                        bt_them_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.save;
                        bt_them_dmkho.Text = "Lưu";
                        dem++;
                        gvkho.Rows.AddNew();
                        trangthai = true;
                        gvkho.CurrentRow.Cells[1].Value = mamoi.ToString();
                        gvkho.Rows[gvkho.Rows.Count - 1].Cells["TenKho"].IsSelected = true;
                    }
                }
                else if (trangthai == true)
                {
                    try
                    {
                        if (them == true && xoa == true && sua == true)
                        {
                            bt_sua_dmkho.Enabled = true;
                            bt_xoa_dmkho.Enabled = true;
                        }
                        else
                        {
                            if (sua == false)
                                bt_sua_dmkho.Enabled = false;
                            else
                                bt_sua_dmkho.Enabled = true;

                            if (xoa == false)
                                bt_xoa_dmkho.Enabled = false;
                            else
                                bt_xoa_dmkho.Enabled = true;
                        }

                        //Them Thong Tin Kho
                        errorProvider1.Clear();
                        for (int i = 0; i < gvkho.Rows.Count; i++)
                        {
                            try
                            {
                                if (!KiemTra_DanhSach.Contains(gvkho.Rows[i].Cells["MaKho"].Value))
                                {
                                    string makho = gvkho.Rows[i].Cells["MaKho"].Value.ToString().Trim();
                                    string tenkho = gvkho.Rows[i].Cells["TenKho"].Value.ToString().Trim();
                                    //int soton = int.Parse(gvkho.Rows[i].Cells["TongSoLuongTon"].Value.ToString().Trim());
                                    if (!xuli.InsertKho(makho, tenkho, 0))
                                    {
                                        MessageBox.Show("Thêm thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                        return;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                logger.Error("Error: " + ex.Message);
                            }


                        }

                        add++;
                        MessageBox.Show("Thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Load_DanhSach_Kho();

                        //giao diện
                        bt_them_dmkho.Image = global::QuanLyNhaSach.Properties.Resources._new;
                        bt_them_dmkho.Text = "Thêm";
                        trangthai = false;
                    }
                    catch (Exception ex)
                    {
                        logger.Error("Error: " + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_sua_dmkho_Click(object sender, EventArgs e)
        {
            try
            {
                //Phan Quyen
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them_dmkho.Enabled = false;
                        bt_xoa_dmkho.Enabled = false;
                    }
                    else
                    {
                        if (them == false)
                            bt_them_dmkho.Enabled = false;
                        else
                            bt_them_dmkho.Enabled = false;

                        if (xoa == false)
                            bt_xoa_dmkho.Enabled = false;
                        else
                            bt_xoa_dmkho.Enabled = true;
                    }
                    // giao dien
                    gvkho.AllowEditRow = true;
                    bt_sua_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_sua_dmkho.Text = "Lưu";
                    trangthai = true;
                }
                else if (trangthai == true)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them_dmkho.Enabled = true;
                        bt_xoa_dmkho.Enabled = true;
                    }
                    else
                    {
                        if (them == false)
                            bt_them_dmkho.Enabled = false;
                        else
                            bt_them_dmkho.Enabled = true;

                        if (xoa == false)
                            bt_xoa_dmkho.Enabled = false;
                        else
                            bt_xoa_dmkho.Enabled = true;
                    }

                    errorProvider1.Clear();
                    // Sua Thong Tin kho
                    string makho = gvkho.CurrentRow.Cells["MaKho"].Value.ToString().Trim();
                    string tenkho = gvkho.CurrentRow.Cells["TenKho"].Value.ToString().Trim();
                    int soton = int.Parse(gvkho.CurrentRow.Cells["TongSoLuongTon"].Value.ToString().Trim());
                    if (!xuli.UpdateKho(makho, tenkho, soton))
                    {
                        MessageBox.Show("Sửa thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    update++;
                    MessageBox.Show("Sửa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_DanhSach_Kho();

                    // giao diện
                    bt_sua_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmkho.Text = "Sửa";
                    gvkho.AllowEditRow = false;
                    trangthai = false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_lamtuoi_dmkho_Click(object sender, EventArgs e)
        {
            Load_DanhSach_Kho();
        }
        private void bt_xoa_dmkho_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn thực sự muốn xoá loại hàng này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string makho = gvkho.CurrentRow.Cells["MaKho"].Value.ToString();
                    if (!xuli.DeleteKho(makho))
                    {
                        MessageBox.Show("Xoá thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                        MessageBox.Show("Xoá thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_DanhSach_Kho();
                    delete++;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_excel_dmkho_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog1.FileName.Equals(String.Empty))
            {
                RadMessageBox.SetThemeName(this.gv_kho.ThemeName);
                RadMessageBox.Show("Làm ơn nhập tên file.");
                return;
            }
            string fileName = this.saveFileDialog1.FileName;

            ExportToExcelML excelExporter = new ExportToExcelML(this.gv_kho);

            excelExporter.SheetMaxRows = ExcelMaxRows._1048576;
            excelExporter.ExportVisualSettings = this.exportVisualSettings;


            try
            {
                excelExporter.RunExport(fileName);

                RadMessageBox.SetThemeName(this.gv_kho.ThemeName);
                DialogResult dr = RadMessageBox.Show("Dữ liệu trong Grid đã được in thành công. Bạn có muốn mở file này không?",
                    "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    openExportFile = true;
                }
            }
            catch (IOException ex)
            {
                RadMessageBox.SetThemeName(this.gv_kho.ThemeName);
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
        private void bt_tim_dmkho_Click(object sender, EventArgs e)
        {
            try
            {
                if (bt_tim_dmkho.Text == "Tìm kiếm")
                {
                    bt_tim_dmkho.Text = "Trở lại";
                    bt_tim_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.go_back_icon;
                    gvkho.EnableFiltering = true;
                    gvkho.ShowFilteringRow = true;
                }
                else if (bt_tim_dmkho.Text == "Trở lại")
                {
                    bt_tim_dmkho.Text = "Tìm kiếm";
                    bt_tim_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.loc;
                    gvkho.EnableFiltering = false;
                    gvkho.ShowFilteringRow = false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
       private void bt_ngung_dmkho_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();

                if (them == false)
                {
                    bt_them_dmkho.Enabled = true;
                    bt_them_dmkho.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them_dmkho.Text = "Thêm";
                    gvkho.AllowAddNewRow = false;
                }
                else
                {
                    bt_them_dmkho.Enabled = true;
                    bt_them_dmkho.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them_dmkho.Text = "Thêm";
                    gvkho.AllowAddNewRow = false;
                }

                if (sua == false)
                {
                    bt_sua_dmkho.Enabled = true;
                    bt_sua_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmkho.Text = "Sửa";
                    gvkho.AllowEditRow = false;
                }
                else
                {
                    bt_sua_dmkho.Enabled = true;
                    bt_sua_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmkho.Text = "Sửa";
                    gvkho.AllowEditRow = false;
                }

                if (xoa == false)
                    bt_xoa_dmkho.Enabled = false;
                else
                    bt_xoa_dmkho.Enabled = true;

                trangthai = false;
                Load_DanhSach_Kho();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        #endregion
        //-------------------------
        #region Tab: Danh muc nha cung cap
        private void Load_DMNhaCungCap()
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                //giao dien
                this.AddSummariesNCC();

                if (them == false)
                    bt_them_dmncc.Enabled = false;
                else
                    bt_them_dmncc.Enabled = true;

                if (sua == false)
                    bt_sua_dmncc.Enabled = false;
                else
                    bt_sua_dmncc.Enabled = true;

                if (xoa == false)
                    bt_xoa_dmncc.Enabled = false;
                else
                    bt_xoa_dmncc.Enabled = true;
                //Load Danh sach nha cung cap
                Load_BangNhaCungCap();
                gv_dmnhacungcap.EnableFiltering = false;
                gv_dmnhacungcap.ShowFilteringRow = false;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void Load_BangNhaCungCap()
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(new StringReader(xuli.selectqueryByText("select mancc,tenncc,diachincc,sodtncc,emailncc,congnoncc from NhaCungCap where mancc <>'NCC0000'")));
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Add("STT", typeof(int));
                    gv_dmnhacungcap.DataSource = ds.Tables[0];
                    DataRow dr;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dr = ds.Tables[0].NewRow();
                        dr["STT"] = i + 1;
                        ds.Tables[0].Rows[i]["STT"] = dr["STT"].ToString();
                        KiemTra_DanhSach.Add(ds.Tables[0].Rows[i]["MaNCC"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void AddSummariesNCC()
        {
            try
            {
                this.gv_dmnhacungcap.MasterTemplate.SummaryRowsTop.Clear();
                this.gv_dmnhacungcap.TableElement.BeginUpdate();
                this.gv_dmnhacungcap.MasterTemplate.EnableFiltering = true;
                this.gv_dmnhacungcap.MasterTemplate.AutoExpandGroups = true;
                this.gv_dmnhacungcap.MasterTemplate.AllowAddNewRow = false;
                this.gv_dmnhacungcap.TableElement.EndUpdate();
                GridViewSummaryRowItem item1 = new GridViewSummaryRowItem();
                item1.Add(new GridViewSummaryItem("TenNCC", "Tổng nhà cung cấp: {0}", GridAggregateFunction.Count));
                this.gv_dmnhacungcap.MasterTemplate.SummaryRowsTop.Add(item1);
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }
        private void bt_lamtuoi_dmncc_Click(object sender, EventArgs e)
        {
            Load_BangNhaCungCap();
        }
        private void bt_them_dmncc_Click(object sender, EventArgs e)
        {
            try
            {
                // string mamoi= gv_dmnhacungcap.CurrentRow.Cells["MaNCC"].Value.ToString().Trim();
                string mamoi = "";
                DataSet ds_tam = new DataSet();
                string maTam = "NCC";
                sv_QuanLyNhaSach.AdapterClient xuly = new sv_QuanLyNhaSach.AdapterClient();
                ds_tam.ReadXml(new StringReader(xuly.selectqueryByText("select top 1 MaNCC from NhaCungCap where MANCC like '%" + maTam + "%'order by MANCC desc")));
                if (ds_tam.Tables.Count == 0)
                    mamoi = maTam + "0001";
                else
                {
                    string matam = ds_tam.Tables[0].Rows[0][0].ToString();
                    string kituso = matam.Substring(3, 4).ToString();
                    int somoi = int.Parse(kituso) + 1;
                    mamoi = maTam + somoi.ToString("0000");
                }
                int dem = 0;
                int vitribatdau = gv_dmnhacungcap.Rows.Count - 1;
                //theo kiểu phân quyền
                if (trangthai == false)
                {

                    if (them == true && xoa == true && sua == true)
                    {
                        bt_sua_dmncc.Enabled = false;
                        bt_xoa_dmncc.Enabled = false;
                    }
                    else
                    {
                        if (sua == false)
                            bt_sua_dmncc.Enabled = false;
                        else
                            bt_sua_dmncc.Enabled = false;

                        if (xoa == false)
                            bt_xoa_dmncc.Enabled = false;
                        else
                            bt_xoa_dmncc.Enabled = true;
                    }

                    //giao dien
                    bt_them_dmncc.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_them_dmncc.Text = "Lưu";
                    dem++;
                    gv_dmnhacungcap.Rows.AddNew();
                    trangthai = true;
                    gv_dmnhacungcap.AllowEditRow = true;
                    gv_dmnhacungcap.CurrentRow.Cells[1].Value = mamoi.ToString();
                    gv_dmnhacungcap.Rows[gv_dmnhacungcap.Rows.Count - 1].Cells["TenNCC"].IsSelected = true;
                }
                else if (trangthai == true)
                {
                    try
                    {
                        if (Regex.IsMatch(gv_dmnhacungcap.CurrentRow.Cells["SoDTNCC"].Value.ToString().Trim(), @"^(\d{8,11})*$") == false)
                        {
                            MessageBox.Show("Số ĐT chỉ nhập số, độ dài từ 8->11 kí tự");
                        }
                        else if (Regex.IsMatch(gv_dmnhacungcap.CurrentRow.Cells["EmailNCC"].Value.ToString().Trim(), @"^([A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4})*$") == false)
                        {
                            MessageBox.Show("Email nhập sai rồi");
                        }
                        else if (Regex.IsMatch(gv_dmnhacungcap.CurrentRow.Cells["CongNoNCC"].Value.ToString().Trim(), @"^(\d)*$") == false)
                        {
                            MessageBox.Show("Công nợ chỉ cho nhập số");
                        }

                        ////Them Thong Tin nha cung cap
                        errorProvider1.Clear();
                        for (int i = vitribatdau; i <= vitribatdau + dem; i++)
                        {


                            if (!KiemTra_DanhSach.Contains(gv_dmnhacungcap.Rows[i].Cells["MaNCC"].Value.ToString()))
                            {
                                // mamoi = gv_dmnhacungcap.Rows[i].Cells["MaNCC"].Value.ToString().Trim();
                                string mancc = mamoi;

                                string tenncc = gv_dmnhacungcap.Rows[i].Cells["TenNCC"].Value.ToString().Trim();
                                string diachincc = gv_dmnhacungcap.Rows[i].Cells["DiaChiNCC"].Value.ToString().Trim();
                                string sodtncc = gv_dmnhacungcap.Rows[i].Cells["SoDTNCC"].Value.ToString().Trim();
                                string emailncc = gv_dmnhacungcap.Rows[i].Cells["EmailNCC"].Value.ToString().Trim();
                                double congnoncc = double.Parse(gv_dmnhacungcap.Rows[i].Cells["CongNoNCC"].Value.ToString());
                                if (!xuli.InsertNCC(mancc, tenncc, diachincc, sodtncc, emailncc, congnoncc))
                                {
                                    MessageBox.Show("Thêm thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    return;
                                }
                            }
                        }
                        add++;
                        MessageBox.Show("Thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Load_BangNhaCungCap();

                        //giao diện
                        bt_them_dmncc.Image = global::QuanLyNhaSach.Properties.Resources._new;
                        bt_them_dmncc.Text = "Thêm";
                        gv_dmnhacungcap.AllowAddNewRow = false;
                        trangthai = false;
                    }
                    catch (Exception ex)
                    {
                        logger.Error("Error: " + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_tim_dmncc_Click(object sender, EventArgs e)
        {

            if (bt_tim_dmncc.Text == "Trở lại")
            {
                gv_dmnhacungcap.EnableFiltering = false;
                gv_dmnhacungcap.ShowFilteringRow = false;
                bt_tim.Text = "Tìm nhà cung cấp";
            }
            else if (bt_tim_dmncc.Text == "Tìm nhà cung cấp")
            {
                gv_dmnhacungcap.EnableFiltering = true;
                gv_dmnhacungcap.ShowFilteringRow = true;
                bt_tim.Text = "Trở lại";
            }

        }
        private void bt_sua_dmncc_Click(object sender, EventArgs e)
        {
            try
            {
                //Phan Quyen
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them_dmncc.Enabled = false;
                        bt_xoa_dmncc.Enabled = false;
                    }
                    else
                    {
                        if (them == false)
                            bt_them_dmncc.Enabled = false;
                        else
                            bt_them_dmncc.Enabled = false;

                        if (xoa == false)
                            bt_xoa_dmncc.Enabled = false;
                        else
                            bt_xoa_dmncc.Enabled = true;
                    }
                    // giao dien
                    bt_sua_dmncc.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    gv_dmnhacungcap.AllowEditRow = true;
                    bt_sua_dmncc.Text = "Lưu";
                    trangthai = true;
                }
                else if (trangthai == true)
                {
                    errorProvider1.Clear();
                    //Sua Thong Tin Nha cung cấp
                    if (Regex.IsMatch(gv_dmnhacungcap.CurrentRow.Cells["SoDTNCC"].Value.ToString().Trim(), @"^(\d{8,11})*$") == false)
                    {
                        MessageBox.Show("Số ĐT chỉ nhập số, độ dài từ 8->11 kí tự");
                    }
                    else if (Regex.IsMatch(gv_dmnhacungcap.CurrentRow.Cells["EmailNCC"].Value.ToString().Trim(), @"^([A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4})*$") == false)
                    {
                        MessageBox.Show("Email nhập sai rồi");
                    }
                    else if (Regex.IsMatch(gv_dmnhacungcap.CurrentRow.Cells["CongNoNCC"].Value.ToString().Trim(), @"^(\d)*$") == false)
                    {
                        MessageBox.Show("Công nợ chỉ cho nhập số");
                    }
                    else
                    {
                        string mancc = gv_dmnhacungcap.CurrentRow.Cells["MaNCC"].Value.ToString().Trim();
                        string tenncc = gv_dmnhacungcap.CurrentRow.Cells["TenNCC"].Value.ToString().Trim();
                        string diachincc = gv_dmnhacungcap.CurrentRow.Cells["DiaChiNCC"].Value.ToString().Trim();
                        string sodtncc = gv_dmnhacungcap.CurrentRow.Cells["SoDTNCC"].Value.ToString().Trim();
                        string emailncc = gv_dmnhacungcap.CurrentRow.Cells["EmailNCC"].Value.ToString().Trim();
                        double congnoncc = double.Parse(gv_dmnhacungcap.CurrentRow.Cells["CongNoNCC"].Value.ToString());
                        if (!xuli.UpdateNCC(mancc, tenncc, diachincc, sodtncc, emailncc, congnoncc))
                        {
                            MessageBox.Show("Sửa thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }

                        update++;
                        MessageBox.Show("Sửa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Load_BangNhaCungCap();

                        // giao diện
                        bt_sua_dmncc.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                        bt_sua_dmncc.Text = "Sửa";
                        trangthai = false;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_xoa_dmncc_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn thực sự muốn xoá nhà cung cấp này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string maloai = gv_dmnhacungcap.CurrentRow.Cells["MaNCC"].Value.ToString();
                    if (!xuli.DeleteNCC(maloai))
                    {
                        MessageBox.Show("Xoá thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                        MessageBox.Show("Xoá thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangNhaCungCap();
                    delete++;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_huy_dmncc_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();

                if (them == false)
                {
                    bt_them_dmncc.Enabled = true;
                    bt_them_dmncc.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them_dmncc.Text = "Thêm";
                    gv_dmnhacungcap.AllowAddNewRow = false;
                }
                else
                {
                    bt_them_dmncc.Enabled = true;
                    bt_them_dmncc.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them_dmncc.Text = "Thêm";
                    gv_dmnhacungcap.AllowAddNewRow = false;
                }

                if (sua == false)
                {
                    bt_sua_dmncc.Enabled = true;
                    bt_sua_dmncc.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmncc.Text = "Sửa";
                    gv_dmnhacungcap.AllowEditRow = false;
                }
                else
                {
                    bt_sua_dmncc.Enabled = true;
                    bt_sua_dmncc.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmncc.Text = "Sửa";
                    gv_dmnhacungcap.AllowEditRow = false;
                }

                if (xoa == false)
                    bt_xoa_dmncc.Enabled = false;
                else
                    bt_xoa_dmncc.Enabled = true;

                trangthai = false;
                Load_BangNhaCungCap();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_excel_dmncc_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog1.FileName.Equals(String.Empty))
            {
                RadMessageBox.SetThemeName(this.gv_dmnhacungcap.ThemeName);
                RadMessageBox.Show("Làm ơn nhập tên file.");
                return;
            }
            string fileName = this.saveFileDialog1.FileName;

            ExportToExcelML excelExporter = new ExportToExcelML(this.gv_dmnhacungcap);

            excelExporter.SheetMaxRows = ExcelMaxRows._1048576;
            excelExporter.ExportVisualSettings = this.exportVisualSettings;


            try
            {
                excelExporter.RunExport(fileName);

                RadMessageBox.SetThemeName(this.gv_dmnhacungcap.ThemeName);
                DialogResult dr = RadMessageBox.Show("Dữ liệu trong Grid đã được in thành công. Bạn có muốn mở file này không?",
                    "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    openExportFile = true;
                }
            }
            catch (IOException ex)
            {
                RadMessageBox.SetThemeName(this.gv_dmnhacungcap.ThemeName);
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }

        #endregion
        //------------------------
        #region Tab: Danh mục nhóm hàng
        private void Load_DMNhomHang()
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                //giao dien
                this.AddSummariesNhomHang();

                if (them == false)
                    bt_them_dmnhomhang.Enabled = false;
                else
                    bt_them_dmnhomhang.Enabled = true;

                if (sua == false)
                    bt_sua_dmnhomhang.Enabled = false;
                else
                    bt_sua_dmnhomhang.Enabled = true;

                if (xoa == false)
                    bt_xoa_dmnhomhang.Enabled = false;
                else
                    bt_xoa_dmnhomhang.Enabled = true;
                //Load Danh sach nha cung cap
                Load_BangNhomHang();
                gv_dmnhomhang.EnableFiltering = false;
                gv_dmnhomhang.ShowFilteringRow = false;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void Load_BangNhomHang()
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(new StringReader(xuli.LayDS_LoaiHang()));
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Add("STT", typeof(int));
                    gv_dmnhomhang.DataSource = ds.Tables[0];
                    DataRow dr;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dr = ds.Tables[0].NewRow();
                        dr["STT"] = i + 1;
                        ds.Tables[0].Rows[i]["STT"] = dr["STT"].ToString();
                        KiemTra_DanhSach.Add(ds.Tables[0].Rows[i]["MaLoai"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void AddSummariesNhomHang()
        {
            this.gv_dmnhomhang.MasterTemplate.SummaryRowsTop.Clear();
            this.gv_dmnhomhang.TableElement.BeginUpdate();
            this.gv_dmnhomhang.MasterTemplate.EnableFiltering = true;
            this.gv_dmnhomhang.MasterTemplate.AutoExpandGroups = true;
            this.gv_dmnhomhang.MasterTemplate.AllowAddNewRow = false;
            this.gv_dmnhomhang.TableElement.EndUpdate();
            GridViewSummaryRowItem item1 = new GridViewSummaryRowItem();
            item1.Add(new GridViewSummaryItem("TenLoai", "Tổng loại hàng: {0}", GridAggregateFunction.Count));
            this.gv_dmnhacungcap.MasterTemplate.SummaryRowsTop.Add(item1);

        }
        private void bt_lamtuoi_dmnhomhang_Click(object sender, EventArgs e)
        {
            Load_BangNhomHang();
        }
        private void bt_them_dmnhomhang_Click(object sender, EventArgs e)
        {
            try
            {
                //theo kiểu phân quyền
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_sua_dmnhomhang.Enabled = false;
                        bt_xoa_dmnhomhang.Enabled = false;
                    }
                    else
                    {
                        if (sua == false)
                            bt_sua_dmnhomhang.Enabled = false;
                        else
                            bt_sua_dmnhomhang.Enabled = false;

                        if (xoa == false)
                            bt_xoa_dmnhomhang.Enabled = false;
                        else
                            bt_xoa_dmnhomhang.Enabled = true;
                    }
                    //giao dien
                    bt_them_dmnhomhang.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_them_dmnhomhang.Text = "Lưu";
                    gv_dmnhomhang.AllowAddNewRow = true;
                    trangthai = true;
                }
                else if (trangthai == true)
                {

                    if (them == true && xoa == true && sua == true)
                    {
                        bt_sua_dmnhomhang.Enabled = true;
                        bt_xoa_dmnhomhang.Enabled = true;
                    }
                    else
                    {
                        if (sua == false)
                            bt_sua_dmnhomhang.Enabled = false;
                        else
                            bt_sua_dmnhomhang.Enabled = true;

                        if (xoa == false)
                            bt_xoa_dmnhomhang.Enabled = false;
                        else
                            bt_xoa_dmnhomhang.Enabled = true;
                    }
                    ////Them Thong Tin Loai Hang
                    errorProvider1.Clear();
                    for (int i = 0; i < gv_dmnhomhang.Rows.Count; i++)
                    {
                        if (!KiemTra_DanhSach.Contains(gv_dmnhomhang.Rows[i].Cells["MaLoai"].Value.ToString()))
                        {
                            string maloai = gv_dmnhomhang.Rows[i].Cells["MaLoai"].Value.ToString().Trim();
                            string tenloai = gv_dmnhomhang.Rows[i].Cells["TenLoai"].Value.ToString().Trim();
                            if (!xuli.InsertLoaiHang(maloai, tenloai))
                            {
                                MessageBox.Show("Thêm thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                        }
                    }
                    add++;
                    MessageBox.Show("Thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangNhomHang();

                    //giao diện
                    bt_them_dmnhomhang.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them_dmnhomhang.Text = "Thêm";
                    gv_dmnhomhang.AllowAddNewRow = false;
                    trangthai = false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_sua_dmnhomhang_Click(object sender, EventArgs e)
        {
            try
            {
                //Phan Quyen
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them_dmnhomhang.Enabled = false;
                        bt_xoa_dmnhomhang.Enabled = false;
                    }
                    else
                    {
                        if (them == false)
                            bt_them_dmnhomhang.Enabled = false;
                        else
                            bt_them_dmnhomhang.Enabled = false;

                        if (xoa == false)
                            bt_xoa_dmnhomhang.Enabled = false;
                        else
                            bt_xoa_dmnhomhang.Enabled = true;
                    }
                    // giao dien
                    gv_dmnhomhang.AllowEditRow = true;
                    bt_sua_dmnhomhang.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_sua_dmnhomhang.Text = "Lưu";
                    trangthai = true;
                }
                else if (trangthai == true)
                {

                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them_dmnhomhang.Enabled = true;
                        bt_xoa_dmnhomhang.Enabled = true;
                    }
                    else
                    {
                        if (them == false)
                            bt_them_dmnhomhang.Enabled = false;
                        else
                            bt_them_dmnhomhang.Enabled = true;

                        if (xoa == false)
                            bt_xoa_dmnhomhang.Enabled = false;
                        else
                            bt_xoa_dmnhomhang.Enabled = true;
                    }
                    errorProvider1.Clear();
                    //Sua Thong Tin Loai Hang
                    string maloai = gv_dmnhomhang.CurrentRow.Cells["MaLoai"].Value.ToString().Trim();
                    string tenloai = gv_dmnhomhang.CurrentRow.Cells["TenLoai"].Value.ToString().Trim();
                    if (!xuli.UpdateLoaiHang(maloai, tenloai))
                    {
                        MessageBox.Show("Sửa thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    update++;
                    MessageBox.Show("Sửa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangNhomHang();

                    // giao diện
                    bt_sua_dmnhomhang.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmnhomhang.Text = "Sửa";
                    gv_dmnhomhang.AllowEditRow = false;
                    trangthai = false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_xoa_dmnhomhang_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn thực sự muốn xoá loại hàng này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string maloai = gv_dmnhomhang.CurrentRow.Cells["MaLoai"].Value.ToString();
                    if (!xuli.DeleteLoaiHang(maloai))
                    {
                        MessageBox.Show("Xoá thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                        MessageBox.Show("Xoá thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangNhomHang();
                    delete++;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_huy_dmnhomhang_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();

                if (them == false)
                {
                    bt_them_dmnhomhang.Enabled = false;
                    bt_them_dmnhomhang.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them_dmnhomhang.Text = "Thêm";
                    gv_dmnhomhang.AllowAddNewRow = false;
                }
                else
                {
                    bt_them_dmnhomhang.Enabled = true;
                    bt_them_dmnhomhang.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them_dmnhomhang.Text = "Thêm";
                    gv_dmnhomhang.AllowAddNewRow = false;
                }

                if (sua == false)
                {
                    bt_sua_dmnhomhang.Enabled = false;
                    bt_sua_dmnhomhang.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmnhomhang.Text = "Sửa";
                    gv_dmnhomhang.AllowEditRow = false;
                }
                else
                {
                    bt_sua_dmnhomhang.Enabled = true;
                    bt_sua_dmnhomhang.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmnhomhang.Text = "Sửa";
                    gv_dmnhomhang.AllowEditRow = false;
                }

                if (xoa == false)
                    bt_xoa_dmnhomhang.Enabled = false;
                else
                    bt_xoa_dmnhomhang.Enabled = true;

                trangthai = false;
                Load_BangNhomHang();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_excel_dmnhomhang_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog1.FileName.Equals(String.Empty))
            {
                RadMessageBox.SetThemeName(this.gv_dmnhomhang.ThemeName);
                RadMessageBox.Show("Làm ơn nhập tên file.");
                return;
            }
            string fileName = this.saveFileDialog1.FileName;

            ExportToExcelML excelExporter = new ExportToExcelML(this.gv_dmnhomhang);

            excelExporter.SheetMaxRows = ExcelMaxRows._1048576;
            excelExporter.ExportVisualSettings = this.exportVisualSettings;


            try
            {
                excelExporter.RunExport(fileName);

                RadMessageBox.SetThemeName(this.gv_dmnhomhang.ThemeName);
                DialogResult dr = RadMessageBox.Show("Dữ liệu trong Grid đã được in thành công. Bạn có muốn mở file này không?",
                    "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    openExportFile = true;
                }
            }
            catch (IOException ex)
            {
                RadMessageBox.SetThemeName(this.gv_dmnhomhang.ThemeName);
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
        private void bt_tim_dmnhomhang_Click(object sender, EventArgs e)
        {
            if (bt_tim_dmnhomhang.Text == "Trở lại")
            {
                gv_dmnhomhang.EnableFiltering = false;
                gv_dmnhomhang.ShowFilteringRow = false;
                bt_tim_dmnhomhang.Text = "Tìm nhóm hàng";
            }
            else if (bt_tim_dmnhomhang.Text == "Tìm nhóm hàng")
            {
                gv_dmnhomhang.EnableFiltering = true;
                gv_dmnhomhang.ShowFilteringRow = true;
                bt_tim_dmnhomhang.Text = "Trở lại";
            }
        }
        #endregion
        //------------------------     
        #region Tab: Danh mục mặt hàng (Phát - chưa xong)
        private void Load_DMHangHoa()
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                //pictureBox1.Image = Image.FromStream(new MemoryStream(xuli.Layhinh("SAGK000013")));
                Load_BangMatHang();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }
        private void bt_lamtuoi_dmmh_Click(object sender, EventArgs e)
        {
            Load_DMHangHoa();
        }
        private void bt_them_dmmh_Click(object sender, EventArgs e)
        {
            //if (Application.OpenForms["FormPhu_MatHang"] == null)
            //{
            FormPhu_MatHang f = new FormPhu_MatHang();
                //f.Name = "FormPhu_MatHang";
                f.tt = "them";
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    Load_BangMatHang();
                }
            //}
        }
        private void Load_BangMatHang()
        {
            DataSet ds = new DataSet();

            try
            {            
                ds.ReadXml(new StringReader(xuli.LayDS_MatHang()));
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Add("stt", typeof(int));
                    DataRow dr;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dr = ds.Tables[0].NewRow();
                        dr["stt"] = i + 1;
                        ds.Tables[0].Rows[i]["stt"] = dr["stt"].ToString();
                        if (ds.Tables[0].Rows[i]["DonGiaNhap"].ToString().Contains(".0000") == true)
                        {
                            ds.Tables[0].Rows[i]["DonGiaNhap"] = ds.Tables[0].Rows[i]["DonGiaNhap"].ToString().Replace(".0000", "");
                        }
                        if (ds.Tables[0].Rows[i]["DonGiaSi"].ToString().Contains(".0000") == true)
                        {
                            ds.Tables[0].Rows[i]["DonGiaSi"] = ds.Tables[0].Rows[i]["DonGiaSi"].ToString().Replace(".0000", "");
                        }
                        if (ds.Tables[0].Rows[i]["DonGiaLe"].ToString().Contains(".0000") == true)
                        {
                            ds.Tables[0].Rows[i]["DonGiaLe"] = ds.Tables[0].Rows[i]["DonGiaLe"].ToString().Replace(".0000", "");
                        }
                    }
                    gv_danhmuchanghoa.DataSource = ds.Tables[0];
                }
                else
                    gv_danhmuchanghoa.DataSource = null;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }


        }
        private void bt_sua_dmmh_Click(object sender, EventArgs e)
        {
            FormPhu_MatHang phu_MH =new FormPhu_MatHang();
            string mamh = gv_danhmuchanghoa.CurrentRow.Cells["MaMH"].Value.ToString();
            phu_MH.mamh_fp = mamh;
            phu_MH.tt = "sua";
            if (phu_MH.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                Load_BangMatHang();
            }
        }
        private void bt_xoa_dmmh_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có thật sự muốn xóa mặt hàng này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string mamh = gv_danhmuchanghoa.CurrentRow.Cells[1].Value.ToString();
                    if (!xuli.DeleteMatHang(mamh))
                    {
                        MessageBox.Show("Xoá thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                        MessageBox.Show("Xoá thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangMatHang();
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void gv_danhmuchanghoa_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(xuli.selectqueryByText("select hinh from mathang"));
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_excel_dmmh_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog1.FileName.Equals(String.Empty))
            {
                RadMessageBox.SetThemeName(this.gv_danhmuchanghoa.ThemeName);
                RadMessageBox.Show("Làm ơn nhập tên file.");
                return;
            }
            string fileName = this.saveFileDialog1.FileName;

            ExportToExcelML excelExporter = new ExportToExcelML(this.gv_danhmuchanghoa);

            excelExporter.SheetMaxRows = ExcelMaxRows._1048576;
            excelExporter.ExportVisualSettings = this.exportVisualSettings;


            try
            {
                excelExporter.RunExport(fileName);

                RadMessageBox.SetThemeName(this.gv_danhmuchanghoa.ThemeName);
                DialogResult dr = RadMessageBox.Show("Dữ liệu trong Grid đã được in thành công. Bạn có muốn mở file này không?",
                    "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    openExportFile = true;
                }
            }
            catch (IOException ex)
            {
                RadMessageBox.SetThemeName(this.gv_danhmuchanghoa.ThemeName);
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
        private void bt_tim_dmmh_Click(object sender, EventArgs e)
        {
            try
            {
                if (bt_tim_dmkho.Text == "Tìm kiếm")
                {
                    bt_tim_dmkho.Text = "Trở lại";
                    bt_tim_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.go_back_icon;
                    gvkho.EnableFiltering = true;
                    gvkho.ShowFilteringRow = true;
                }
                else if (bt_tim_dmkho.Text == "Trở lại")
                {
                    bt_tim_dmkho.Text = "Tìm kiếm";
                    bt_tim_dmkho.Image = global::QuanLyNhaSach.Properties.Resources.loc;
                    gvkho.EnableFiltering = false;
                    gvkho.ShowFilteringRow = false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        #endregion
        //------------------------
        #region Tab: Danh mục khách hàng
        private void Load_DMKhachHang()
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                //giao dien

                this.AddSummariesKhachHang();


                if (them == false)
                    bt_them_dmkhachhang.Enabled = false;
                else
                    bt_them_dmkhachhang.Enabled = true;

                if (sua == false)
                    bt_sua_dmkhachhang.Enabled = false;
                else
                    bt_sua_dmkhachhang.Enabled = true;

                if (xoa == false)
                    bt_xoa_dmkhachhang.Enabled = false;
                else
                    bt_xoa_dmkhachhang.Enabled = true;

                Load_BangKhachHang();

                gv_dmKH.EnableFiltering = false;
                gv_dmKH.ShowFilteringRow = false;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void Load_BangKhachHang()
        {
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(new StringReader(xuli.selectqueryByText("select makh,hotenkh,diachikh,sodtkh,emailkh,sofax,congnokh,masothuekh from KhachHang where MaKh <> 'KH0000'")));
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Add("stt", typeof(int));
                    gv_dmKH.DataSource = ds.Tables[0];
                    DataRow dr;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dr = ds.Tables[0].NewRow();
                        dr["stt"] = i + 1;
                        ds.Tables[0].Rows[i]["stt"] = dr["stt"].ToString();
                        KiemTra_DanhSach.Add(ds.Tables[0].Rows[i]["MaKH"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void AddSummariesKhachHang()
        {
            this.gv_dmKH.MasterTemplate.SummaryRowsTop.Clear();
            this.gv_dmKH.TableElement.BeginUpdate();
            this.gv_dmKH.MasterTemplate.EnableFiltering = true;
            this.gv_dmKH.MasterTemplate.AutoExpandGroups = true;
            this.gv_dmKH.MasterTemplate.AllowAddNewRow = false;
            this.gv_dmKH.TableElement.EndUpdate();
            GridViewSummaryRowItem item1 = new GridViewSummaryRowItem();
            item1.Add(new GridViewSummaryItem("HoTenKH", "Tổng khách hàng: {0}", GridAggregateFunction.Count));
            this.gv_dmKH.MasterTemplate.SummaryRowsTop.Add(item1);
        }
        private void bt_lamtuoi_dmkhachhang_Click(object sender, EventArgs e)
        {
            Load_BangKhachHang();
        }
        private void bt_them_dmkhachhang_Click(object sender, EventArgs e)
        {
            DataSet ds_tam = new DataSet();
            string mamoi = "";
            string maTam = "KH";
            try
            {
                sv_QuanLyNhaSach.AdapterClient xuly = new sv_QuanLyNhaSach.AdapterClient();
                ds_tam.ReadXml(new StringReader(xuly.selectqueryByText("select top 1 MaKH from KHACHHANG where MAKH like '%" + maTam + "%'order by MAKH desc")));
                if (ds_tam.Tables.Count == 0)
                    mamoi = maTam + "0001";
                else
                {
                    string matam = ds_tam.Tables[0].Rows[0][0].ToString();
                    string kituso = matam.Substring(2, 4).ToString();
                    int somoi = int.Parse(kituso) + 1;
                    mamoi = maTam + somoi.ToString("0000");
                }
                int dem = 0;
                int vitribatdau = gv_dmKH.Rows.Count - 1;


                FormPhu_KhachHang frm = new FormPhu_KhachHang();
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    Load_BangKhachHang();
                }

                dem++;
                gv_dmKH.CurrentRow.Cells[1].Value = mamoi.ToString();
                gv_dmKH.Rows[gv_dmKH.Rows.Count - 1].Cells["HoTenKH"].IsSelected = true;

            }

            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_sua_dmkhachhang_Click(object sender, EventArgs e)
        {
            try
            {
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them_dmkhachhang.Enabled = false;
                        bt_xoa_dmkhachhang.Enabled = false;
                    }
                    else
                    {
                        if (them == false)
                            bt_them_dmkhachhang.Enabled = false;
                        else
                            bt_them_dmkhachhang.Enabled = false;

                        if (xoa == false)
                            bt_xoa_dmkhachhang.Enabled = false;
                        else
                            bt_xoa_dmkhachhang.Enabled = true;
                    }
                    //giao dien
                    bt_sua_dmkhachhang.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_sua_dmkhachhang.Text = "Lưu";
                    trangthai = true;
                    gv_dmKH.ReadOnly = false;
                }
                else if (trangthai == true)
                {
                    //errorProvider1.Clear();
                    //Sua Thong Tin Khach Hang
                    string makh = gv_dmKH.CurrentRow.Cells["MaKH"].Value.ToString().Trim();
                    string tenkh = gv_dmKH.CurrentRow.Cells["HoTenKH"].Value.ToString().Trim();
                    string sdt = gv_dmKH.CurrentRow.Cells["SoDTKH"].Value.ToString().Trim();
                    string diachi = gv_dmKH.CurrentRow.Cells["DiaChiKH"].Value.ToString().Trim();
                    string email = gv_dmKH.CurrentRow.Cells["EmailKH"].Value.ToString().Trim();
                    int sofax = int.Parse(gv_dmKH.CurrentRow.Cells["SoFax"].Value.ToString().Trim());
                    double congno = double.Parse(gv_dmKH.CurrentRow.Cells["CongNoKH"].Value.ToString().Trim());
                    string masothue = gv_dmKH.CurrentRow.Cells["MaSoThueKH"].Value.ToString().Trim();
                    if (!xuli.UpdateKH(makh, tenkh, sdt, diachi, email, sofax, congno, masothue))
                    {
                        MessageBox.Show("Sửa thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    update++;
                    MessageBox.Show("Sửa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangKhachHang();

                    //giao diện
                    bt_sua_dmkhachhang.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmkhachhang.Text = "Sửa";
                    trangthai = false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_xoa_dmkhachhang_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn thực sự muốn xoá khách hàng này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string makh = gv_dmKH.CurrentRow.Cells["MaKH"].Value.ToString();
                    if (!xuli.DeleteKH(makh))
                    {
                        MessageBox.Show("Xoá thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                        MessageBox.Show("Xoá thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_BangKhachHang();
                    delete++;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_huy_dmkhachhang_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();

                if (them == false)
                {
                    bt_them_dmkhachhang.Enabled = false;
                    bt_them_dmkhachhang.Image = global::QuanLyNhaSach.Properties.Resources._new;
                    bt_them_dmkhachhang.Text = "Thêm";
                    gv_dmKH.AllowAddNewRow = false;
                }
                else
                    bt_them_dmkhachhang.Enabled = true;

                if (sua == false)
                {
                    bt_sua_dmkhachhang.Enabled = false;
                    bt_sua_dmkhachhang.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                    bt_sua_dmkhachhang.Text = "Sửa";
                    gv_dmKH.AllowEditRow = false;
                }
                else
                    bt_sua_dmkhachhang.Enabled = true;

                if (xoa == false)
                    bt_xoa_dmkhachhang.Enabled = false;
                else
                    bt_xoa_dmkhachhang.Enabled = true;

                trangthai = false;
                Load_BangKhachHang();
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private void bt_excel_dmkhachhang_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog1.FileName.Equals(String.Empty))
            {
                RadMessageBox.SetThemeName(this.gv_dmKH.ThemeName);
                RadMessageBox.Show("Làm ơn nhập tên file.");
                return;
            }
            string fileName = this.saveFileDialog1.FileName;

            ExportToExcelML excelExporter = new ExportToExcelML(this.gv_dmKH);

            excelExporter.SheetMaxRows = ExcelMaxRows._1048576;
            excelExporter.ExportVisualSettings = this.exportVisualSettings;


            try
            {
                excelExporter.RunExport(fileName);

                RadMessageBox.SetThemeName(this.gv_dmKH.ThemeName);
                DialogResult dr = RadMessageBox.Show("Dữ liệu trong Grid đã được in thành công. Bạn có muốn mở file này không?",
                    "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    openExportFile = true;
                }
            }
            catch (IOException ex)
            {
                RadMessageBox.SetThemeName(this.gv_dmKH.ThemeName);
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
        private void bt_tim_dmkhachhang_Click(object sender, EventArgs e)
        {
            if (bt_tim_dmkhachhang.Text == "Trở lại")
            {
                gv_dmKH.EnableFiltering = false;
                gv_dmKH.ShowFilteringRow = false;
                bt_tim_dmkhachhang.Text = "Tìm khách hàng";
            }
            else if (bt_tim_dmkhachhang.Text == "Tìm khách hàng")
            {
                gv_dmKH.EnableFiltering = true;
                gv_dmKH.ShowFilteringRow = true;
                bt_tim_dmkhachhang.Text = "Trở lại";
            }
        }
        #endregion
        //----------------------
        #region Tab: Thông tin người dùng
        private void Load_Thongtinnguoidung()
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(new StringReader(xuli.selectqueryByText("select * from NhanVien nv, TaiKhoanNV tk where tk.MaNV= nv.MaNV and nv.MaNV='" + belbl_MaNV.Text.Trim() + "'")));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    txt_ttnd_hoten.Text = ds.Tables[0].Rows[0]["HoTenNV"].ToString();
                    txt_ttnd_ngaysinh.Text = ds.Tables[0].Rows[0]["NgaySinhNV"].ToString();
                    txt_ttng_sodt.Text = ds.Tables[0].Rows[0]["SoDTNV"].ToString();
                    txt_ttnd_diachi.Text = ds.Tables[0].Rows[0]["DiaChiNV"].ToString();
                    txt_ttnd_cmnd.Text = ds.Tables[0].Rows[0]["CMNDNV"].ToString();
                    txt_ttnd_chucvu.Text = ds.Tables[0].Rows[0]["ChucVu"].ToString();
                    lb_manv.Text = belbl_MaNV.Text.Trim();
                    lb_tentaikhoan.Text = ds.Tables[0].Rows[0]["MaTKNV"].ToString();

                }
            }
            catch
            {
            }
        }
        #endregion
        #region "Ham chuyen so thanh chu"
        private static string Chu(string gNumber)
        {
            string result = "";
            switch (gNumber)
            {
                case "0":
                    result = "không";
                    break;
                case "1":
                    result = "một";
                    break;
                case "2":
                    result = "hai";
                    break;
                case "3":
                    result = "ba";
                    break;
                case "4":
                    result = "bốn";
                    break;
                case "5":
                    result = "năm";
                    break;
                case "6":
                    result = "sáu";
                    break;
                case "7":
                    result = "bảy";
                    break;
                case "8":
                    result = "tám";
                    break;
                case "9":
                    result = "chín";
                    break;
            }
            return result;
        }
        private static string Donvi(string so)
        {
            string Kdonvi = "";

            if (so.Equals("1"))
                Kdonvi = "";
            if (so.Equals("2"))
                Kdonvi = "nghìn";
            if (so.Equals("3"))
                Kdonvi = "triệu";
            if (so.Equals("4"))
                Kdonvi = "tỷ";
            if (so.Equals("5"))
                Kdonvi = "nghìn tỷ";
            if (so.Equals("6"))
                Kdonvi = "triệu tỷ";
            if (so.Equals("7"))
                Kdonvi = "tỷ tỷ";

            return Kdonvi;
        }
        private static string Tach(string tach3)
        {

            string Ktach = "";
            if (tach3.Equals("000"))
                return "";
            if (tach3.Length == 3)
            {
                try
                {
                    string tr = tach3.Trim().Substring(0, 1).ToString().Trim();
                    string ch = tach3.Trim().Substring(1, 1).ToString().Trim();
                    string dv = tach3.Trim().Substring(2, 1).ToString().Trim();
                    if (tr.Equals("0") && ch.Equals("0"))
                        Ktach = " không trăm lẻ " + Chu(dv.ToString().Trim()) + " ";
                    if (!tr.Equals("0") && ch.Equals("0") && dv.Equals("0"))
                        Ktach = Chu(tr.ToString().Trim()).Trim() + " trăm ";
                    if (!tr.Equals("0") && ch.Equals("0") && !dv.Equals("0"))
                        Ktach = Chu(tr.ToString().Trim()).Trim() + " trăm lẻ " + Chu(dv.Trim()).Trim() + " ";
                    if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                        Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi " + Chu(dv.Trim()).Trim() + " ";
                    if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && dv.Equals("0"))
                        Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi ";
                    if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && dv.Equals("5"))
                        Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi lăm ";
                    if (tr.Equals("0") && ch.Equals("1") && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                        Ktach = " không trăm mười " + Chu(dv.Trim()).Trim() + " ";
                    if (tr.Equals("0") && ch.Equals("1") && dv.Equals("0"))
                        Ktach = " không trăm mười ";
                    if (tr.Equals("0") && ch.Equals("1") && dv.Equals("5"))
                        Ktach = " không trăm mười lăm ";
                    if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                        Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi " + Chu(dv.Trim()).Trim() + " ";
                    if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && dv.Equals("0"))
                        Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi ";
                    if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && dv.Equals("5"))
                        Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi lăm ";
                    if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                        Ktach = Chu(tr.Trim()).Trim() + " trăm mười " + Chu(dv.Trim()).Trim() + " ";

                    if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && dv.Equals("0"))
                        Ktach = Chu(tr.Trim()).Trim() + " trăm mười ";
                    if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && dv.Equals("5"))
                        Ktach = Chu(tr.Trim()).Trim() + " trăm mười lăm ";
                }
                catch
                {
                }

            }
            return Ktach;

        }
        public static string So_chu(double gNum)
        {
            if (gNum == 0)
                return "Không đồng";

            string lso_chu = "";
            string tach_mod = "";
            string tach_conlai = "";
            double Num = Math.Round(gNum, 0);
            string gN = Convert.ToString(Num);
            int m = Convert.ToInt32(gN.Length / 3);
            int mod = gN.Length - m * 3;
            string dau = "[+]";

            // Dau [+ , - ]
            if (gNum < 0)
                dau = "[-]";
            dau = "";

            // Tach hang lon nhat
            if (mod.Equals(1))
                tach_mod = "00" + Convert.ToString(Num.ToString().Trim().Substring(0, 1)).Trim();
            if (mod.Equals(2))
                tach_mod = "0" + Convert.ToString(Num.ToString().Trim().Substring(0, 2)).Trim();
            if (mod.Equals(0))
                tach_mod = "000";
            // Tach hang con lai sau mod :
            if (Num.ToString().Length > 2)
                tach_conlai = Convert.ToString(Num.ToString().Trim().Substring(mod, Num.ToString().Length - mod)).Trim();

            ///don vi hang mod 
            int im = m + 1;
            if (mod > 0)
                lso_chu = Tach(tach_mod).ToString().Trim() + " " + Donvi(im.ToString().Trim());
            /// Tach 3 trong tach_conlai

            int i = m;
            int _m = m;
            int j = 1;
            string tach3 = "";
            string tach3_ = "";

            while (i > 0)
            {
                tach3 = tach_conlai.Trim().Substring(0, 3).Trim();
                tach3_ = tach3;
                lso_chu = lso_chu.Trim() + " " + Tach(tach3.Trim()).Trim();
                m = _m + 1 - j;
                if (!tach3_.Equals("000"))
                    lso_chu = lso_chu.Trim() + " " + Donvi(m.ToString().Trim()).Trim();
                tach_conlai = tach_conlai.Trim().Substring(3, tach_conlai.Trim().Length - 3);

                i = i - 1;
                j = j + 1;
            }
            if (lso_chu.Trim().Substring(0, 1).Equals("k"))
                lso_chu = lso_chu.Trim().Substring(10, lso_chu.Trim().Length - 10).Trim();
            if (lso_chu.Trim().Substring(0, 1).Equals("l"))
                lso_chu = lso_chu.Trim().Substring(2, lso_chu.Trim().Length - 2).Trim();
            if (lso_chu.Trim().Length > 0)
                lso_chu = dau.Trim() + " " + lso_chu.Trim().Substring(0, 1).Trim().ToUpper() + lso_chu.Trim().Substring(1, lso_chu.Trim().Length - 1).Trim() + " đồng chẵn.";

            return lso_chu.ToString().Trim();

        }
        #endregion
        #endregion

        #region "Các Tab ConTROL CHỌN NHANH"
        private void btnTracuu_dathangncc_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmPhieuDatHangNCC rt = frmPhieuDatHangNCC.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_khdathang_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmPhieuDatHang rt = frmPhieuDatHang.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_khtrahang_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmHangBanTraLai rt = frmHangBanTraLai.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_trahangncc_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmTraHangNhaCungCap rt = frmTraHangNhaCungCap.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_dieuchinhkho_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmDieuChinhKho rt = frmDieuChinhKho.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_hoadonnhap_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmHoaDonMuaHang hdmh = frmHoaDonMuaHang.TaoDoiTuong();
            hdmh.Activate();
            hdmh.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_hoadonbansi_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmHoaDonBanSi rt = frmHoaDonBanSi.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_hoadonbanle_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmHoaDonBanLe rt = frmHoaDonBanLe.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_phieuthu_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmPhieuThu rt = frmPhieuThu.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }
        private void btnTracuu_phieuchi_Quick_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmPhieuChi rt = frmPhieuChi.TaoDoiTuong();
            rt.Activate();
            rt.Show();
            this.Cursor = Cursors.Default;
        }

        //-----------------------------------------
        private void btnTracuu_dathangncc_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_dathangncc_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_dathangncc_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_dathangncc_Quick_MouseLeave(object sender, EventArgs e)
        {
            this.btnTracuu_dathangncc_Quick.BackgroundImage = null;
        }
        private void btnTracuu_dathangncc_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_dathangncc_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_dathangncc_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_khdathang_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_khdathang_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_khdathang_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_khdathang_Quick_MouseLeave(object sender, EventArgs e)
        {
            this.btnTracuu_khdathang_Quick.BackgroundImage = null;
        }
        private void btnTracuu_khdathang_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_khdathang_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_khdathang_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_khtrahang_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_khtrahang_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_khtrahang_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_khtrahang_Quick_MouseLeave(object sender, EventArgs e)
        {
            btnTracuu_khtrahang_Quick.BackgroundImage = null;
        }
        private void btnTracuu_khtrahang_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_khtrahang_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_khtrahang_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_trahangncc_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_trahangncc_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_trahangncc_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_trahangncc_Quick_MouseLeave(object sender, EventArgs e)
        {
            btnTracuu_trahangncc_Quick.BackgroundImage = null;
        }
        private void btnTracuu_trahangncc_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_trahangncc_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_trahangncc_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_dieuchinhkho_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_dieuchinhkho_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_dieuchinhkho_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_dieuchinhkho_Quick_MouseLeave(object sender, EventArgs e)
        {
            btnTracuu_dieuchinhkho_Quick.BackgroundImage = null;
        }
        private void btnTracuu_dieuchinhkho_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_dieuchinhkho_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_dieuchinhkho_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_hoadonnhap_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_hoadonnhap_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_hoadonnhap_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_hoadonnhap_Quick_MouseLeave(object sender, EventArgs e)
        {
            btnTracuu_hoadonnhap_Quick.BackgroundImage = null;
        }
        private void btnTracuu_hoadonnhap_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_hoadonnhap_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_hoadonnhap_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_hoadonbansi_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_hoadonbansi_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_hoadonbansi_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_hoadonbansi_Quick_MouseLeave(object sender, EventArgs e)
        {
            btnTracuu_hoadonbansi_Quick.BackgroundImage = null;
        }
        private void btnTracuu_hoadonbansi_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_hoadonbansi_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_hoadonbansi_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_hoadonbanle_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_hoadonbanle_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_hoadonbanle_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_hoadonbanle_Quick_MouseLeave(object sender, EventArgs e)
        {
            btnTracuu_hoadonbanle_Quick.BackgroundImage = null;
        }
        private void btnTracuu_hoadonbanle_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_hoadonbanle_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_hoadonbanle_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_phieuthu_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_phieuthu_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_phieuthu_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_phieuthu_Quick_MouseLeave(object sender, EventArgs e)
        {
            btnTracuu_phieuthu_Quick.BackgroundImage = null;
        }
        private void btnTracuu_phieuthu_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_phieuthu_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_phieuthu_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnTracuu_phieuchi_Quick_MouseHover(object sender, EventArgs e)
        {
            btnTracuu_phieuchi_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_phieuchi_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void btnTracuu_phieuchi_Quick_MouseLeave(object sender, EventArgs e)
        {
            btnTracuu_phieuchi_Quick.BackgroundImage = null;
        }
        private void btnTracuu_phieuchi_Quick_MouseMove(object sender, MouseEventArgs e)
        {
            btnTracuu_phieuchi_Quick.BackgroundImage = global::QuanLyNhaSach.Properties.Resources.nenbutton;
            btnTracuu_phieuchi_Quick.BackgroundImageLayout = ImageLayout.Stretch;
        }
        #endregion

        private bool kiem_traPhanQuyen(string chuoi_kiem_tra)
        {
            return chuoi_kiem_tra != "Off" ? true : false;
        }  
        private void gv_chitietphanquyen_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            try
            {
                if (cmb_nhomquyen.Text.Trim() == "")
                {

                }
                else
                {
                    DataRow dr;
                    if (gv_chitietphanquyen.Rows.Count > 0)
                    {
                        if (bang_tam.Rows.Count > 0)
                            for (int i = 0; i < bang_tam.Rows.Count; i++)
                            {
                                if (bang_tam.Rows[i]["MaForm"].ToString().Trim() == e.OldRow.Cells["MaForm"].Value.ToString().Trim())
                                    bang_tam.Rows[i].Delete();
                                //return;
                            }
                        dr = bang_tam.NewRow();
                        dr["MaNhom"] = e.OldRow.Cells["MaNhom"].Value;
                        dr["MaForm"] = e.OldRow.Cells["MaForm"].Value;
                        dr["Them"] = kiem_traPhanQuyen(e.OldRow.Cells["Them"].Value.ToString());
                        dr["Xoa"] = kiem_traPhanQuyen(e.OldRow.Cells["Xoa"].Value.ToString());
                        dr["Sua"] = kiem_traPhanQuyen(e.OldRow.Cells["Sua"].Value.ToString());
                        dr["ToanQuyen"] = kiem_traPhanQuyen(e.OldRow.Cells["Toanquyen"].Value.ToString());
                        bang_tam.Rows.Add(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        private void gv_danhmuchanghoa_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            try
            {
                byte[] hinh=xuli.Layhinh(e.CurrentRow.Cells["mamh"].Value.ToString());
                if (hinh != null)
                {
                    hinh = (byte[])Decompress(hinh);
                    pictureBox1.Visible = true;
                    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                    pictureBox1.Image = Image.FromStream(new MemoryStream(hinh));
                    node5.Text = "Loại hàng: " + TenLoaiMH(e.CurrentRow.Cells["MaLoai"].Value.ToString().Trim());
                    node1.Text = "Nhà sản xuất: " + e.CurrentRow.Cells["NhaSX"].Value.ToString();
                    node2.Text = "Năm xuất bản: " + e.CurrentRow.Cells["NamXuatBan"].Value.ToString();
                    node3.Text = "Tác giả: " + e.CurrentRow.Cells["TacGia"].Value.ToString();
                    node4.Text = "Đầu sách: " + e.CurrentRow.Cells["DauSach"].Value.ToString();
                }
                else
                {
                    pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
                    node5.Text = "Loại hàng: " + TenLoaiMH(e.CurrentRow.Cells["MaLoai"].Value.ToString().Trim());
                    node1.Text = "Nhà sản xuất: " + e.CurrentRow.Cells["NhaSX"].Value.ToString();
                    node2.Text = "Năm xuất bản: " + e.CurrentRow.Cells["NamXuatBan"].Value.ToString();
                    node3.Text = "Tác giả: " + e.CurrentRow.Cells["TacGia"].Value.ToString();
                    node4.Text = "Đầu sách: " + e.CurrentRow.Cells["DauSach"].Value.ToString();
                    pictureBox1.Image = global::QuanLyNhaSach.Properties.Resources.close2;
                }
            }
            catch (Exception ex)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
                node5.Text = "Loại hàng: " + TenLoaiMH(e.CurrentRow.Cells["MaLoai"].Value.ToString().Trim());
                node1.Text = "Nhà sản xuất: " + e.CurrentRow.Cells["NhaSX"].Value.ToString();
                node2.Text = "Năm xuất bản: " + e.CurrentRow.Cells["NamXuatBan"].Value.ToString();
                node3.Text = "Tác giả: " + e.CurrentRow.Cells["TacGia"].Value.ToString();
                node4.Text = "Đầu sách: " + e.CurrentRow.Cells["DauSach"].Value.ToString();
                pictureBox1.Image = global::QuanLyNhaSach.Properties.Resources.close2;
                logger.Error("Error: " + ex.Message);
            }
        }
        private string TenLoaiMH(string maloai)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(xuli.selectqueryByText("select tenloai from loaihang where maloai like'"+maloai+"%'")));
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }
        private object Decompress(byte[] gzip)
        {
            using (DeflateStream stream = new DeflateStream(new MemoryStream(gzip), CompressionMode.Decompress))
            {
                const int size = 4096;
                byte[] buffer = new byte[size];
                using (MemoryStream memory = new MemoryStream())
                {
                    int count = 0;
                    do
                    {
                        count = stream.Read(buffer, 0, size);
                        if (count > 0)
                        {
                            memory.Write(buffer, 0, count);
                        }
                    }
                    while (count > 0);
                    var dataOut = memory.ToArray();
                    MemoryStream _MemoryStreamb = new MemoryStream(dataOut);
                    BinaryFormatter _BinaryFormatterb = new BinaryFormatter();
                    // Serializes an object, or graph of connected objects, to the given stream.
                    return _BinaryFormatterb.Deserialize(_MemoryStreamb);
                }
            }
        }

        private void radButtonElement9_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmDSDatHangOnline dsonl = frmDSDatHangOnline.TaoDoiTuong();
            dsonl.Activate();
            dsonl.Show();
            this.Cursor = Cursors.Default;
        }
    }
}


